Skip to content

Commit

Permalink
preserve error data on throws
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxOhn committed Sep 23, 2024
1 parent a679fbc commit e956150
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/args/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl MapOrAttrs {
return Ok(Self::Map(map));
}

Err(JsError::from(
Err(JsError::new(
"Expected either previously calculated attributes or a beatmap",
))
}
Expand Down
2 changes: 1 addition & 1 deletion src/attributes/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,6 @@ impl TryFrom<JsDifficultyAttributes> for DifficultyAttributes {
}
}

Err(JsError::from("invalid difficulty attributes"))
Err(JsError::new("invalid difficulty attributes"))
}
}
4 changes: 2 additions & 2 deletions src/beatmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl JsBeatmap {
e = src;
}

Err(JsError::new(content))
Err(JsError::new(&content))
}
}
}
Expand All @@ -60,7 +60,7 @@ impl JsBeatmap {
let mode = GameMode::from(mode);

if let ConvertStatus::Incompatible = self.inner.convert_in_place(mode) {
return Err(JsError::new(format!(
return Err(JsError::new(&format!(
"Cannot convert {:?} to {mode:?}",
self.inner.mode
)));
Expand Down
2 changes: 1 addition & 1 deletion src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'de, 'js> de::Deserializer<'de> for JsDeserializer<'js> {
.is_some_and(|name| name == struct_name);

if !correct_classname {
return Err(JsError::new(format!("Expected {struct_name}")));
return Err(JsError::new(&format!("Expected {struct_name}")));
}

visitor.visit_map(ObjectAccess::new(obj, fields))
Expand Down
30 changes: 8 additions & 22 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,39 @@ use std::{error, fmt};
use serde::de;
use wasm_bindgen::prelude::*;

use crate::util;

pub type JsResult<T> = Result<T, JsError>;

#[derive(Debug)]
pub struct JsError(JsValue);
pub struct JsError(js_sys::Error);

impl JsError {
pub fn new(err: String) -> Self {
Self(err.into())
}
}

impl From<&'static str> for JsError {
fn from(err: &'static str) -> Self {
Self(util::static_str_to_js(err).into())
pub fn new(msg: &str) -> Self {
Self(js_sys::Error::new(msg))
}
}

impl From<JsValue> for JsError {
fn from(value: JsValue) -> Self {
Self(value)
Self(value.into())
}
}

impl From<JsError> for JsValue {
fn from(JsError(value): JsError) -> Self {
value
fn from(JsError(err): JsError) -> Self {
err.into()
}
}

impl fmt::Display for JsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = String)]
pub fn to_string(value: &JsValue) -> String;
}

to_string(&self.0).fmt(f)
self.0.to_string().fmt(f)
}
}

impl error::Error for JsError {}

impl de::Error for JsError {
fn custom<T: fmt::Display>(msg: T) -> Self {
JsError::new(msg.to_string())
JsError::new(&msg.to_string())
}
}

0 comments on commit e956150

Please sign in to comment.