Skip to content

Commit 01572f0

Browse files
committed
feat(sourcemap): impl std::fmt::Display for Error (#3902)
1 parent 77a4a0b commit 01572f0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

crates/oxc_sourcemap/src/error.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{error, fmt};
2+
13
#[derive(Debug)]
24
pub enum Error {
35
/// a VLQ string was malformed and data was left over
@@ -15,6 +17,35 @@ pub enum Error {
1517
/// a reference to a non existing name was encountered
1618
BadNameReference(u32),
1719
}
20+
impl fmt::Display for Error {
21+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22+
match self {
23+
Error::VlqLeftover => write!(f, "VLQ string was malformed and data was left over"),
24+
Error::VlqNoValues => write!(f, "VLQ string was empty and no values could be decoded"),
25+
Error::VlqOverflow => write!(f, "The input encoded a number that didn't fit into i64"),
26+
Error::BadJson(err) => write!(f, "JSON parsing error: {err}"),
27+
Error::BadSegmentSize(size) => {
28+
write!(f, "Mapping segment had an unsupported size of {size}")
29+
}
30+
Error::BadSourceReference(idx) => {
31+
write!(f, "Reference to non-existing source at position {idx}")
32+
}
33+
Error::BadNameReference(idx) => {
34+
write!(f, "Reference to non-existing name at position {idx}")
35+
}
36+
}
37+
}
38+
}
39+
40+
impl error::Error for Error {
41+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
42+
if let Self::BadJson(err) = self {
43+
Some(err)
44+
} else {
45+
None
46+
}
47+
}
48+
}
1849

1950
/// The result of decoding.
2051
pub type Result<T> = std::result::Result<T, Error>;

0 commit comments

Comments
 (0)