Skip to main content

swc_sourcemap/
errors.rs

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