diff --git a/components/script/body.rs b/components/script/body.rs index 35ee2dae2c6..1ae05e4b043 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -16,8 +16,10 @@ use js::jsapi::Heap; use js::jsapi::JSContext; use js::jsapi::JSObject; use js::jsapi::JS_ClearPendingException; +use js::jsapi::JS_GetPendingException; use js::jsapi::JS_ParseJSON; use js::jsapi::Value as JSValue; +use js::jsval::JSVal; use js::jsval::UndefinedValue; use js::typedarray::{ArrayBuffer, CreateWith}; use mime::{Mime, TopLevel, SubLevel}; @@ -42,6 +44,7 @@ pub enum FetchedData { BlobData(DomRoot), FormData(DomRoot), ArrayBuffer(RootedTraceableBox>), + JSException(RootedTraceableBox>) } // https://fetch.spec.whatwg.org/#concept-body-consume-body @@ -90,7 +93,8 @@ pub fn consume_body_with_promise(object: &T, FetchedData::Json(j) => promise.resolve_native(&j), FetchedData::BlobData(b) => promise.resolve_native(&b), FetchedData::FormData(f) => promise.resolve_native(&f), - FetchedData::ArrayBuffer(a) => promise.resolve_native(&a) + FetchedData::ArrayBuffer(a) => promise.resolve_native(&a), + FetchedData::JSException(e) => promise.reject_native(&e.handle()), }; }, Err(err) => promise.reject_error(err), @@ -133,9 +137,10 @@ fn run_json_data_algorithm(cx: *mut JSContext, json_text.as_ptr(), json_text.len() as u32, rval.handle_mut()) { + rooted!(in(cx) let mut exception = UndefinedValue()); + assert!(JS_GetPendingException(cx, exception.handle_mut())); JS_ClearPendingException(cx); - // TODO: See issue #13464. Exception should be thrown instead of cleared. - return Err(Error::Type("Failed to parse JSON".to_string())); + return Ok(FetchedData::JSException(RootedTraceableBox::from_box(Heap::boxed(exception.get())))); } let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(rval.get())); Ok(FetchedData::Json(rooted_heap)) diff --git a/tests/wpt/metadata/fetch/api/request/request-consume.html.ini b/tests/wpt/metadata/fetch/api/request/request-consume.html.ini index 611766b22ac..8ec3d996158 100644 --- a/tests/wpt/metadata/fetch/api/request/request-consume.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-consume.html.ini @@ -6,9 +6,6 @@ [Consume String request's body as formData] expected: FAIL - [Trying to consume bad JSON text as JSON: 'undefined'] - expected: FAIL - [Consume Int8Array request's body as text] expected: FAIL @@ -45,15 +42,6 @@ [Consume FormData request's body as FormData] expected: FAIL - [Trying to consume bad JSON text as JSON: 'undefined'] - expected: FAIL - - [Trying to consume bad JSON text as JSON: '{'] - expected: FAIL - - [Trying to consume bad JSON text as JSON: 'a'] - expected: FAIL - [Consume ArrayBuffer request's body as blob] expected: FAIL @@ -81,6 +69,3 @@ [Consume Float32Array request's body as text] expected: FAIL - [Trying to consume bad JSON text as JSON: '['] - expected: FAIL -