File: error_chain.rs

package info (click to toggle)
rust-snafu 0.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 724 kB
  • sloc: sh: 15; makefile: 4
file content (59 lines) | stat: -rw-r--r-- 1,681 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
56
57
58
59
use snafu::prelude::*;
use std::fmt::Debug;

#[derive(Debug, Clone, Snafu)]
enum LeafError {
    #[snafu(display("User ID {} is invalid", user_id))]
    InvalidUser { user_id: i32 },
    #[snafu(display("no user available"))]
    MissingUser,
}

#[derive(Debug, Clone, Snafu)]
enum MiddleError {
    #[snafu(display("failed to check the user"))]
    CheckUser { source: LeafError },
}

#[derive(Debug, Clone, Snafu)]
enum Error {
    #[snafu(display("access control failure"))]
    AccessControl { source: MiddleError },
}

#[track_caller]
fn assert_eq_debug(a: impl Debug, b: impl Debug) {
    assert_eq!(format!("{:?}", a), format!("{:?}", b));
}

#[cfg(feature="std")]
#[test]
fn chain_compat_iterates() {
    use snafu::{ChainCompat, IntoError};

    let bottom_error = InvalidUserSnafu { user_id: 12 }.build();
    let middle_error = CheckUserSnafu.into_error(bottom_error.clone());
    let error = AccessControlSnafu.into_error(middle_error.clone());

    let errors: Vec<_> = ChainCompat::new(&error).collect();

    assert_eq_debug(&errors[0], &error);
    assert_eq_debug(&errors[1], &middle_error);
    assert_eq_debug(&errors[2], &bottom_error);
}

#[cfg(feature="std")]
#[test]
fn errorcompat_chain_iterates() {
    use snafu::{ErrorCompat, IntoError};

    let bottom_error = InvalidUserSnafu { user_id: 12 }.build();
    let middle_error = CheckUserSnafu.into_error(bottom_error.clone());
    let error = AccessControlSnafu.into_error(middle_error.clone());

    let errors: Vec<_> = ErrorCompat::iter_chain(&error).collect();

    assert_eq_debug(&errors[0], &error);
    assert_eq_debug(&errors[1], &middle_error);
    assert_eq_debug(&errors[2], &bottom_error);
}