sourcemap/
errors.rs

1use std::error;
2use std::fmt;
3use std::io;
4use std::str;
5use std::string;
6
7/// Represents results from this library
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// Represents different failure cases
11#[derive(Debug)]
12pub enum Error {
13    /// a std::io error
14    Io(io::Error),
15    #[cfg(feature = "ram_bundle")]
16    /// a scroll error
17    Scroll(scroll::Error),
18    /// a std::str::Utf8Error
19    Utf8(str::Utf8Error),
20    /// a JSON parsing related failure
21    BadJson(serde_json::Error),
22    /// a VLQ string was malformed and data was left over
23    VlqLeftover,
24    /// a VLQ string was empty and no values could be decoded.
25    VlqNoValues,
26    /// Overflow in Vlq handling
27    VlqOverflow,
28    /// a mapping segment had an unsupported size
29    BadSegmentSize(u32),
30    /// a reference to a non existing source was encountered
31    BadSourceReference(u32),
32    /// a reference to a non existing name was encountered
33    BadNameReference(u32),
34    /// Indicates that an incompatible sourcemap format was encountered
35    IncompatibleSourceMap,
36    /// Indicates an invalid data URL
37    InvalidDataUrl,
38    /// Flatten failed
39    CannotFlatten(String),
40    /// The magic of a RAM bundle did not match
41    InvalidRamBundleMagic,
42    /// The RAM bundle index was malformed
43    InvalidRamBundleIndex,
44    /// A RAM bundle entry was invalid
45    InvalidRamBundleEntry,
46    /// Tried to operate on a non RAM bundle file
47    NotARamBundle,
48    /// Range mapping index is invalid
49    InvalidRangeMappingIndex(data_encoding::DecodeError),
50
51    InvalidBase64(char),
52}
53
54impl From<io::Error> for Error {
55    fn from(err: io::Error) -> Error {
56        Error::Io(err)
57    }
58}
59
60#[cfg(feature = "ram_bundle")]
61impl From<scroll::Error> for Error {
62    fn from(err: scroll::Error) -> Self {
63        Error::Scroll(err)
64    }
65}
66
67impl From<string::FromUtf8Error> for Error {
68    fn from(err: string::FromUtf8Error) -> Error {
69        From::from(err.utf8_error())
70    }
71}
72
73impl From<str::Utf8Error> for Error {
74    fn from(err: str::Utf8Error) -> Error {
75        Error::Utf8(err)
76    }
77}
78
79impl From<serde_json::Error> for Error {
80    fn from(err: serde_json::Error) -> Error {
81        Error::BadJson(err)
82    }
83}
84
85impl From<data_encoding::DecodeError> for Error {
86    fn from(err: data_encoding::DecodeError) -> Error {
87        Error::InvalidRangeMappingIndex(err)
88    }
89}
90
91impl error::Error for Error {
92    fn cause(&self) -> Option<&dyn error::Error> {
93        match *self {
94            Error::Io(ref err) => Some(err),
95            #[cfg(feature = "ram_bundle")]
96            Error::Scroll(ref err) => Some(err),
97            Error::Utf8(ref err) => Some(err),
98            Error::BadJson(ref err) => Some(err),
99            _ => None,
100        }
101    }
102}
103
104impl fmt::Display for Error {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        match *self {
107            Error::Io(ref msg) => write!(f, "{msg}"),
108            Error::Utf8(ref msg) => write!(f, "{msg}"),
109            Error::BadJson(ref err) => write!(f, "bad json: {err}"),
110            #[cfg(feature = "ram_bundle")]
111            Error::Scroll(ref err) => write!(f, "parse error: {err}"),
112            Error::VlqLeftover => write!(f, "leftover cur/shift in vlq decode"),
113            Error::VlqNoValues => write!(f, "vlq decode did not produce any values"),
114            Error::VlqOverflow => write!(f, "vlq decode caused an overflow"),
115            Error::BadSegmentSize(size) => write!(f, "got {size} segments, expected 4 or 5"),
116            Error::BadSourceReference(id) => write!(f, "bad reference to source #{id}"),
117            Error::BadNameReference(id) => write!(f, "bad reference to name #{id}"),
118            Error::IncompatibleSourceMap => write!(f, "encountered incompatible sourcemap format"),
119            Error::InvalidDataUrl => write!(f, "the provided data URL is invalid"),
120            Error::CannotFlatten(ref msg) => {
121                write!(f, "cannot flatten the indexed sourcemap: {msg}")
122            }
123            Error::InvalidRamBundleMagic => write!(f, "invalid magic number for ram bundle"),
124            Error::InvalidRamBundleIndex => write!(f, "invalid module index in ram bundle"),
125            Error::InvalidRamBundleEntry => write!(f, "invalid ram bundle module entry"),
126            Error::NotARamBundle => write!(f, "not a ram bundle"),
127            Error::InvalidRangeMappingIndex(err) => write!(f, "invalid range mapping index: {err}"),
128            Error::InvalidBase64(c) => write!(f, "invalid base64 character: {}", c),
129        }
130    }
131}