app.rs 1.25 KB
Newer Older
1
use error_example::{operation1, operation2, AllErrors, Error1, Error2, LE};
2
3
4
5
6
7
8

#[derive(Debug)]
enum UserError {
    LibAllErrors(AllErrors),
    UtfError(std::str::Utf8Error),
}

9
10
impl<T: Into<AllErrors>> From<LE<T>> for UserError {
    fn from(all: LE<T>) -> Self {
11
12
13
14
15
        UserError::LibAllErrors(all.error().into())
    }
}

impl From<std::str::Utf8Error> for UserError {
16
    fn from(all: std::str::Utf8Error) -> Self {
17
18
19
20
        UserError::UtfError(all)
    }
}

21
fn forward_error() -> Result<(), UserError> {
22
    operation1()?;
23
    operation2()?;
24
25
26
    Ok(())
}

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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(())
}