candid/
error.rs

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! `candid::Result<T> = Result<T, candid::Error>>`

use serde::{de, ser};
use std::collections::TryReserveError;
use std::{io, num::ParseIntError};
use thiserror::Error;

pub type Result<T = ()> = std::result::Result<T, Error>;

#[derive(Debug)]
pub struct Label {
    pos: usize,
    message: String,
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("binary parser error: {}", .0.first().map_or_else(|| "io error".to_string(), |f| format!("{} at byte offset {}", f.message, f.pos/2)))]
    Binread(Vec<Label>),

    #[error(transparent)]
    Reserve(#[from] TryReserveError),

    #[error("Subtyping error: {0}")]
    Subtype(String),

    #[error(transparent)]
    Custom(#[from] anyhow::Error),
}

impl Error {
    pub fn msg<T: ToString>(msg: T) -> Self {
        Error::Custom(anyhow::anyhow!(msg.to_string()))
    }
    pub fn subtype<T: ToString>(msg: T) -> Self {
        Error::Subtype(msg.to_string())
    }
}

fn get_binread_labels(e: &binread::Error) -> Vec<Label> {
    use binread::Error::*;
    match e {
        BadMagic { pos, .. } => {
            let pos = (pos * 2) as usize;
            vec![Label {
                pos,
                message: "Unexpected bytes".to_string(),
            }]
        }
        Custom { pos, err } => {
            let pos = (pos * 2) as usize;
            let err = err
                .downcast_ref::<&str>()
                .unwrap_or(&"unknown error (there's a bug in error reporting)");
            vec![Label {
                pos,
                message: (*err).to_string(),
            }]
        }
        EnumErrors {
            pos,
            variant_errors,
        } => {
            let pos = (pos * 2) as usize;
            let variant = variant_errors
                .iter()
                .find(|(_, e)| !matches!(e, BadMagic { .. }));
            // Should have at most one non-magic error
            match variant {
                None => vec![Label {
                    pos,
                    message: "Unknown opcode".to_string(),
                }],
                Some((id, e)) => {
                    let mut labels = get_binread_labels(e);
                    labels.push(Label {
                        pos,
                        message: (*id).to_string(),
                    });
                    labels
                }
            }
        }
        NoVariantMatch { pos } => {
            let pos = (pos * 2) as usize;
            vec![Label {
                pos,
                message: "No variant match".to_string(),
            }]
        }
        AssertFail { pos, message } => {
            let pos = (pos * 2) as usize;
            vec![Label {
                pos,
                message: message.to_string(),
            }]
        }
        Io(_) => vec![],
        _ => unreachable!(),
    }
}

impl ser::Error for Error {
    fn custom<T: std::fmt::Display>(msg: T) -> Self {
        Error::msg(format!("Serialize error: {msg}"))
    }
}

impl de::Error for Error {
    fn custom<T: std::fmt::Display>(msg: T) -> Self {
        let msg = msg.to_string();
        if let Some(msg) = msg.strip_prefix("Subtyping error: ") {
            Error::Subtype(msg.to_string())
        } else {
            Error::msg(format!("Deserialize error: {msg}"))
        }
    }
    fn invalid_type(_: de::Unexpected<'_>, exp: &dyn de::Expected) -> Self {
        Error::Subtype(format!("{exp}"))
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Error {
        Error::msg(format!("io error: {e}"))
    }
}

impl From<binread::Error> for Error {
    fn from(e: binread::Error) -> Error {
        Error::Binread(get_binread_labels(&e))
    }
}

impl From<ParseIntError> for Error {
    fn from(e: ParseIntError) -> Error {
        Error::msg(format!("ParseIntError: {e}"))
    }
}