use std::io;
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum SourceMapErrorType {
UnexpectedNegativeNumber = 1,
UnexpectedlyBigNumber = 2,
VlqUnexpectedEof = 3,
VlqInvalidBase64 = 4,
VlqOverflow = 5,
IOError = 6,
NameOutOfRange = 7,
SourceOutOfRange = 8,
BufferError = 9,
InvalidFilePath = 10,
FromUtf8Error = 11,
}
#[derive(Debug)]
pub struct SourceMapError {
pub error_type: SourceMapErrorType,
pub reason: Option<String>,
}
impl SourceMapError {
pub fn new(error_type: SourceMapErrorType) -> Self {
Self {
error_type,
reason: None,
}
}
pub fn new_with_reason(error_type: SourceMapErrorType, reason: &str) -> Self {
Self {
error_type,
reason: Some(String::from(reason)),
}
}
}
impl From<vlq::Error> for SourceMapError {
#[inline]
fn from(e: vlq::Error) -> SourceMapError {
match e {
vlq::Error::UnexpectedEof => SourceMapError::new(SourceMapErrorType::VlqUnexpectedEof),
vlq::Error::InvalidBase64(_) => {
SourceMapError::new(SourceMapErrorType::VlqInvalidBase64)
}
vlq::Error::Overflow => SourceMapError::new(SourceMapErrorType::VlqOverflow),
}
}
}
impl From<io::Error> for SourceMapError {
#[inline]
fn from(_err: io::Error) -> SourceMapError {
SourceMapError::new(SourceMapErrorType::IOError)
}
}
#[cfg(feature = "native")]
impl From<SourceMapError> for napi::Error {
#[inline]
fn from(err: SourceMapError) -> napi::Error {
let mut reason = String::from("[parcel-sourcemap] ");
match err.error_type {
SourceMapErrorType::UnexpectedNegativeNumber => {
reason.push_str("Unexpected Negative Number");
}
SourceMapErrorType::UnexpectedlyBigNumber => {
reason.push_str("Unexpected Big Number");
}
SourceMapErrorType::VlqUnexpectedEof => {
reason.push_str("VLQ Unexpected end of file");
}
SourceMapErrorType::VlqInvalidBase64 => {
reason.push_str("VLQ Invalid Base 64 value");
}
SourceMapErrorType::VlqOverflow => {
reason.push_str("VLQ Value overflowed, does not fit in u32");
}
SourceMapErrorType::IOError => {
reason.push_str("IO Error");
}
SourceMapErrorType::NameOutOfRange => {
reason.push_str("Name out of range");
}
SourceMapErrorType::SourceOutOfRange => {
reason.push_str("Source out of range");
}
SourceMapErrorType::InvalidFilePath => {
reason.push_str("Invalid FilePath");
}
SourceMapErrorType::BufferError => {
reason.push_str("Something went wrong while writing/reading a sourcemap buffer");
}
SourceMapErrorType::FromUtf8Error => {
reason.push_str("Could not convert utf-8 array to string");
}
}
if let Some(r) = err.reason {
reason.push_str(", ");
reason.push_str(&r[..]);
}
napi::Error::new(napi::Status::GenericFailure, reason)
}
}
#[cfg(feature = "wasm")]
impl From<SourceMapError> for wasm_bindgen::JsValue {
#[inline]
fn from(err: SourceMapError) -> wasm_bindgen::JsValue {
let mut reason = String::from("[parcel-sourcemap] ");
match err.error_type {
SourceMapErrorType::UnexpectedNegativeNumber => {
reason.push_str("Unexpected Negative Number");
}
SourceMapErrorType::UnexpectedlyBigNumber => {
reason.push_str("Unexpected Big Number");
}
SourceMapErrorType::VlqUnexpectedEof => {
reason.push_str("VLQ Unexpected end of file");
}
SourceMapErrorType::VlqInvalidBase64 => {
reason.push_str("VLQ Invalid Base 64 value");
}
SourceMapErrorType::VlqOverflow => {
reason.push_str("VLQ Value overflowed, does not fit in u32");
}
SourceMapErrorType::IOError => {
reason.push_str("IO Error");
}
SourceMapErrorType::NameOutOfRange => {
reason.push_str("Name out of range");
}
SourceMapErrorType::SourceOutOfRange => {
reason.push_str("Source out of range");
}
SourceMapErrorType::InvalidFilePath => {
reason.push_str("Invalid FilePath");
}
SourceMapErrorType::BufferError => {
reason.push_str("Something went wrong while writing/reading a sourcemap buffer");
}
SourceMapErrorType::FromUtf8Error => {
reason.push_str("Could not convert utf-8 array to string");
}
}
if let Some(r) = err.reason {
reason.push_str(", ");
reason.push_str(&r[..]);
}
js_sys::Error::new(&reason).into()
}
}
impl From<rkyv::Unreachable> for SourceMapError {
#[inline]
fn from(_err: rkyv::Unreachable) -> SourceMapError {
SourceMapError::new(SourceMapErrorType::BufferError)
}
}
impl From<std::string::FromUtf8Error> for SourceMapError {
#[inline]
fn from(_err: std::string::FromUtf8Error) -> SourceMapError {
SourceMapError::new(SourceMapErrorType::FromUtf8Error)
}
}