Skip to content
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
26 changes: 24 additions & 2 deletions core/engine/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Error-related types and conversions.

use std::{error, fmt};

use crate::{
builtins::{error::ErrorObject, Array},
js_string,
Expand All @@ -12,6 +10,7 @@ use crate::{
};
use boa_gc::{custom_trace, Finalize, Trace};
use boa_macros::js_str;
use std::{error, fmt};
use thiserror::Error;

/// The error type returned by all operations related
Expand Down Expand Up @@ -146,6 +145,29 @@ impl JsError {
}
}

/// Creates a new `JsError` from a Rust standard error `err`.
/// This will create a new `JsNativeError` with the message of the standard error.
///
/// # Examples
///
/// ```
/// # use boa_engine::JsError;
/// let error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!");
/// let js_error: JsError = JsError::from_rust(error);
///
/// assert_eq!(js_error.as_native().unwrap().message(), "oh no!");
/// assert!(js_error.as_native().unwrap().cause().is_none());
/// ```
#[must_use]
pub fn from_rust(err: impl error::Error) -> Self {
let mut native_err = JsNativeError::error().with_message(err.to_string());
if let Some(source) = err.source() {
native_err = native_err.with_cause(Self::from_rust(source));
}

Self::from_native(native_err)
}

/// Creates a new `JsError` from an opaque error `value`.
///
/// # Examples
Expand Down