Skip to content

Commit

Permalink
impl error for jserror
Browse files Browse the repository at this point in the history
  • Loading branch information
andrieshiemstra committed Oct 2, 2022
1 parent 45f60f8 commit e0c43c8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.5.5

impl Error for JsError

# 0.5.4

* use owned string for DefaultAtom instantiation
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hirofa_utils"
version = "0.5.4"
version = "0.5.5"
authors = ["HiRoFa <[email protected]>"]
edition = "2018"
description = "Utils project which is depended on by several other projects"
Expand Down
10 changes: 0 additions & 10 deletions src/js_utils/adapters/promises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ where
Ok(val_ref) => {
prom_ref
.js_promise_resolve(realm, &val_ref)
.ok()
.expect("prom resolution failed");
}
Err(err) => {
let err_ref = realm
.js_error_create(err.get_name(), err.get_message(), err.get_stack())
.ok()
.expect("could not create str");
prom_ref
.js_promise_reject(realm, &err_ref)
.ok()
.expect("prom rejection failed");
}
}
Expand All @@ -71,11 +68,9 @@ where
// todo use error:new_error(err)
let err_ref = realm
.js_error_create(err.get_name(), err.get_message(), err.get_stack())
.ok()
.expect("could not create str");
prom_ref
.js_promise_reject(realm, &err_ref)
.ok()
.expect("prom rejection failed");
}
}
Expand Down Expand Up @@ -138,17 +133,14 @@ pub(crate) fn new_resolving_promise_async<P, R, M, T>(
Ok(val_ref) => {
prom_ref
.js_promise_resolve(realm, &val_ref)
.ok()
.expect("prom resolution failed");
}
Err(err) => {
let err_ref = realm
.js_error_create(err.get_name(), err.get_message(), err.get_stack())
.ok()
.expect("could not create err");
prom_ref
.js_promise_reject(realm, &err_ref)
.ok()
.expect("prom rejection failed");
}
}
Expand All @@ -157,11 +149,9 @@ pub(crate) fn new_resolving_promise_async<P, R, M, T>(
// todo use error:new_error(err)
let err_ref = realm
.js_error_create(err.get_name(), err.get_message(), err.get_stack())
.ok()
.expect("could not create str");
prom_ref
.js_promise_reject(realm, &err_ref)
.ok()
.expect("prom rejection failed");
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/js_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub trait ScriptPreProcessor {
fn process(&self, script: &mut Script) -> Result<(), JsError>;
}

#[derive(Debug)]
pub struct JsError {
name: String,
message: String,
Expand Down Expand Up @@ -48,13 +49,25 @@ impl JsError {
}
}

impl std::error::Error for JsError {
fn description(&self) -> &str {
self.get_message()
}
}

impl std::fmt::Display for JsError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let e = format!("{}: {}\n{}", self.name, self.message, self.stack);
f.write_str(e.as_str())
}
}

impl From<Error> for JsError {
fn from(e: Error) -> Self {
JsError::new_string(format!("{}", e))
}
}

pub struct Script {
path: String,
code: String,
Expand Down

0 comments on commit e0c43c8

Please sign in to comment.