Commit b1c157d8 authored by tglman's avatar tglman
Browse files

minor changes in naming, add error trait implementation to errors

parent 9b9067ca
......@@ -7,3 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
thiserror="1.0"
use error_example::{AllErrors,operation1,operation2, Error2,ER::ER};
use error_example::{operation1, operation2, AllErrors, Error1, Error2, LE};
#[derive(Debug)]
enum UserError {
......@@ -6,22 +6,50 @@ enum UserError {
UtfError(std::str::Utf8Error),
}
impl<T:Into<AllErrors>> From<ER<T>> for UserError {
fn from(all:ER<T>) -> Self {
impl<T: Into<AllErrors>> From<LE<T>> for UserError {
fn from(all: LE<T>) -> Self {
UserError::LibAllErrors(all.error().into())
}
}
impl From<std::str::Utf8Error> for UserError {
fn from(all:std::str::Utf8Error) -> Self {
fn from(all: std::str::Utf8Error) -> Self {
UserError::UtfError(all)
}
}
fn main() -> Result<(), UserError> {
fn forward_error() -> Result<(), UserError> {
operation1()?;
if let Err(ER(Error2::Error2)) = operation2() {
}
operation2()?;
Ok(())
}
fn handle_error1() {
// This case need to handle only the operation1 errors ignoring all the rest
match operation1() {
Ok(_) => {}
Err(LE::LE(Error1::Error1)) => {
println!("handled error 1");
}
}
}
fn handle_error2() {
// This case need to handle only the operation2 errors ignoring all the rest
match operation2() {
Ok(_) => {}
Err(LE::LE(Error2::Error2)) => {
println!("handled error 2");
}
Err(LE::LE(Error2::Error3(msg))) => {
println!("handled error 3 {}", msg);
}
}
}
fn main() -> Result<(), UserError> {
handle_error1();
handle_error2();
forward_error()?;
Ok(())
}
use std::convert::From;
use thiserror::Error;
pub enum ER<T:Into<AllErrors>>{
ER(T)
pub enum LE<T: Into<AllErrors>> {
LE(T),
}
impl<T:Into<AllErrors>> ER<T> {
pub fn error(self) ->T {
impl<T: Into<AllErrors>> LE<T> {
pub fn error(self) -> T {
match self {
ER::ER(a) => a,
LE::LE(a) => a,
}
}
}
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum AllErrors {
#[error("Error1")]
Error1,
#[error("Error2")]
Error2,
Error3,
#[error("Error3:{0}")]
Error3(String),
}
#[derive(Debug, Error)]
pub enum Error1 {
Error1
#[error("Error1")]
Error1,
}
impl From<Error1> for AllErrors {
fn from(_: Error1) -> Self {
AllErrors::Error1
}
}
}
#[derive(Debug, Error)]
pub enum Error2 {
#[error("Error2")]
Error2,
Error3
#[error("Error3: {0}")]
Error3(String),
}
impl From<Error2> for AllErrors {
fn from(error2: Error2) -> Self {
match error2 {
Error2::Error2 => AllErrors::Error2,
Error2::Error3 => AllErrors::Error3,
Error2::Error3(msg) => AllErrors::Error3(msg),
}
}
}
}
pub fn operation1() -> Result<(), ER<Error1>> {
Err(ER::ER(Error1::Error1))
pub fn operation1() -> Result<(), LE<Error1>> {
Err(LE::LE(Error1::Error1))
}
pub fn operation2() -> Result<(), ER<Error2>> {
Err(ER::ER(Error2::Error2))
pub fn operation2() -> Result<(), LE<Error2>> {
Err(LE::LE(Error2::Error2))
}
pub fn operation3() -> Result<(), ER<Error2>> {
Err(ER::ER(Error2::Error3))
pub fn operation3() -> Result<(), LE<Error2>> {
Err(LE::LE(Error2::Error3("A error happened".to_owned())))
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment