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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::io;
use std::str;
use std::ffi;

#[cfg(feature = "with_dwarf")]
use gimli;
#[cfg(feature = "with_objects")]
use goblin;
#[cfg(feature = "with_objects")]
use scroll;
#[cfg(feature = "with_sourcemaps")]
use sourcemap;

error_chain! {
    errors {
        /// Raised in some cases if panics are caught
        Panic(message: String) {
            description("panic")
            display("panic: {}", message)
        }
        /// Raised on operations that work on symbols if the symbol
        /// data is bad.
        BadSymbol(message: String) {
            description("bad symbol")
            display("bad symbol: {}", &message)
        }
        /// Raised for internal errors in the libraries.  Should not happen.
        Internal(message: &'static str) {
            description("internal error")
            display("internal error: {}", message)
        }
        /// Raised for bad input on parsing in symbolic.
        Parse(message: &'static str) {
            description("parse error")
            display("parse error: {}", message)
        }
        /// General error for missing information.
        NotFound(message: &'static str) {
            description("not found")
            display("not found: {}", message)
        }
        /// Raised for bad input on parsing in symbolic.
        Format(message: &'static str) {
            description("format error")
            display("format error: {}", message)
        }
        /// Raised if unsupported object files are loaded.
        UnsupportedObjectFile {
            description("unsupported object file")
        }
        /// Raised if object files are malformed.
        MalformedObjectFile(message: String) {
            description("malformed object file")
            display("malformed object file: {}", &message)
        }
        /// Raised for unknown cache file versions.
        BadCacheFile(msg: &'static str) {
            description("bad cache file")
            display("bad cache file: {}", msg)
        }
        /// Raised if a section is missing in an object file.
        MissingSection(section: &'static str) {
            description("missing object section")
            display("missing object section '{}'", section)
        }
        /// Raised for DWARF failures.
        BadDwarfData(message: &'static str) {
            description("bad dwarf data")
            display("bad dwarf data: {}", message)
        }
        /// Raised for bad breakpad symbols.
        BadBreakpadSym(msg: &'static str) {
            description("bad breakpad sym")
            display("bad breakpad sym: {}", msg)
        }
        MissingDebugInfo(message: &'static str) {
            description("missing debug info")
            display("missing debug info: {}", message)
        }
        BadSymbolTable(message: &'static str) {
            description("bad symbol table")
            display("bad symbol table: {}", message)
        }
        /// Raised for JSON parse errors.
        BadJson(msg: String) {
            description("bad json")
            display("bad json: {}", &msg)
        }
        /// Raised for bad sourcemap data.
        BadSourcemap(msg: String) {
            description("bad sourcemap")
            display("bad sourcemap: {}", &msg)
        }
        /// Raised while stackwalking minidumps.
        Stackwalk(message: String) {
            description("stackwalking error")
            display("stackwalking error: {}", message)
        }
        /// Raised when a resolver cannot load its symbols file.
        Resolver(message: String) {
            description("resolver error")
            display("resolver error: {}", message)
        }
        /// Raised if sourcemaps cannot be flattened.
        CannotFlattenSourcemap(msg: String) {
            description("cannot flatten sourcemap")
            display("cannot flatten sourcemap: {}", &msg)
        }
    }

    foreign_links {
        Io(io::Error);
        Utf8Error(str::Utf8Error);
        ParseInt(::std::num::ParseIntError);
    }
}

#[cfg(feature = "with_dwarf")]
impl From<gimli::Error> for Error {
    fn from(err: gimli::Error) -> Error {
        use std::mem;
        use std::error::Error;
        // this works because gimli error only returns static strings. UUUGLY
        ErrorKind::BadDwarfData(unsafe { mem::transmute(err.description()) }).into()
    }
}

#[cfg(feature = "with_objects")]
impl From<goblin::error::Error> for Error {
    fn from(err: goblin::error::Error) -> Error {
        use goblin::error::Error::*;
        match err {
            Malformed(s) => ErrorKind::MalformedObjectFile(s).into(),
            BadMagic(m) => ErrorKind::MalformedObjectFile(format!("bad magic: {}", m)).into(),
            Scroll(err) => Error::from(err),
            IO(err) => Error::from(err),
        }
    }
}

#[cfg(feature = "with_objects")]
impl From<scroll::Error> for Error {
    fn from(err: scroll::Error) -> Error {
        use scroll::Error::*;
        match err {
            TooBig { .. } => io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Tried to read type that was too large",
            ).into(),
            BadOffset(..) => io::Error::new(io::ErrorKind::InvalidData, "Bad offset").into(),
            BadInput { .. } => io::Error::new(io::ErrorKind::InvalidData, "Bad input").into(),
            Custom(s) => io::Error::new(io::ErrorKind::Other, s).into(),
            IO(err) => Error::from(err),
        }
    }
}

#[cfg(feature = "with_sourcemaps")]
impl From<sourcemap::Error> for Error {
    fn from(err: sourcemap::Error) -> Error {
        use sourcemap::Error::*;
        match err {
            Io(err) => Error::from(err),
            Utf8(err) => Error::from(err),
            BadJson(err) => Error::from(ErrorKind::BadJson(err.to_string())),
            CannotFlatten(msg) => Error::from(ErrorKind::CannotFlattenSourcemap(msg)),
            err => Error::from(ErrorKind::BadSourcemap(err.to_string())),
        }
    }
}

impl From<ffi::NulError> for Error {
    fn from(_err: ffi::NulError) -> Error {
        ErrorKind::Internal("unexpected null byte in c-string").into()
    }
}