Skip to content

Commit

Permalink
propogate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
n14little committed May 26, 2020
1 parent fadb75c commit 600e560
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 3 additions & 4 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,13 @@ pub fn stringify(_: &mut Value, args: &[Value], interpreter: &mut Interpreter) -
key.to_owned(),
Property::default().value(
interpreter
.call(replacer, &mut this_arg, &[Value::string(key), val.clone()])
.expect("failed to get returned value from replacer function"),
.call(replacer, &mut this_arg, &[Value::string(key), val.clone()])?,
),
);
}
Value::from(object_to_return.to_json().to_string())
Ok(Value::from(object_to_return.to_json().to_string()))
})
.ok_or_else(Value::undefined)
.ok_or_else(Value::undefined)?
} else if replacer_as_object.kind == ObjectKind::Array {
let mut obj_to_return =
serde_json::Map::with_capacity(replacer_as_object.properties.len() - 1);
Expand Down
12 changes: 12 additions & 0 deletions boa/src/builtins/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,15 @@ fn json_stringify_array_converts_symbol_to_null() {

assert_eq!(actual, expected);
}
#[test]
fn json_stringify_function_replacer_propogate_error() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let actual = forward(&mut engine, r#"JSON.stringify({x: 1}, (key, value) => {
throw new TypeError("type error")
})"#);
let expected = forward(&mut engine, r#"'Error: undefined'"#);

assert_eq!(actual, expected);
}

0 comments on commit 600e560

Please sign in to comment.