Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sourcemap): impl std::fmt::Display for Error #3902

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions crates/oxc_sourcemap/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{error, fmt};

#[derive(Debug)]
pub enum Error {
DonIsaac marked this conversation as resolved.
Show resolved Hide resolved
/// a VLQ string was malformed and data was left over
Expand All @@ -15,6 +17,35 @@ pub enum Error {
/// a reference to a non existing name was encountered
BadNameReference(u32),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::VlqLeftover => write!(f, "VLQ string was malformed and data was left over"),
Error::VlqNoValues => write!(f, "VLQ string was empty and no values could be decoded"),
Error::VlqOverflow => write!(f, "The input encoded a number that didn't fit into i64"),
Error::BadJson(err) => write!(f, "JSON parsing error: {err}"),
Error::BadSegmentSize(size) => {
write!(f, "Mapping segment had an unsupported size of {size}")
}
Error::BadSourceReference(idx) => {
write!(f, "Reference to non-existing source at position {idx}")
}
Error::BadNameReference(idx) => {
write!(f, "Reference to non-existing name at position {idx}")
}
}
}
}

impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
if let Self::BadJson(err) = self {
Some(err)
} else {
None
}
}
}

/// The result of decoding.
pub type Result<T> = std::result::Result<T, Error>;
Expand Down
Loading