From c6bf07ec6d231d29149a2454d3b5135a41c6cbab Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Mon, 14 Mar 2022 19:35:15 +0100 Subject: fix(core): Don't override structured clone error messages from V8 (#13942) In the implementation of structured serialization in `Deno.core.serialize`, whenever there is a serialization error, an exception will be thrown with the message "Failed to serialize response", even though V8 provides a message to use in such cases. This change instead throws an exception with the V8-provided message, if there is one. --- core/bindings.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/bindings.rs b/core/bindings.rs index 4e5c68675..2ab55187d 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -898,7 +898,7 @@ impl<'a> v8::ValueSerializerImpl for SerializeDeserialize<'a> { scope: &mut v8::HandleScope<'s>, message: v8::Local<'s, v8::String>, ) { - let error = v8::Exception::error(scope, message); + let error = v8::Exception::type_error(scope, message); scope.throw_exception(error); } @@ -1101,15 +1101,25 @@ fn serialize( } } - match value_serializer.write_value(scope.get_current_context(), value) { - Some(true) => { + let must_throw = { + let scope = &mut v8::TryCatch::new(scope); + let ret = value_serializer.write_value(scope.get_current_context(), value); + if scope.has_caught() || scope.has_terminated() { + scope.rethrow(); + false + } else if let Some(true) = ret { let vector = value_serializer.release(); let zbuf: ZeroCopyBuf = vector.into(); rv.set(to_v8(scope, zbuf).unwrap()); + false + } else { + // We throw the TypeError outside of the v8::TryCatch scope. + true } - _ => { - throw_type_error(scope, "Failed to serialize response"); - } + }; + + if must_throw { + throw_type_error(scope, "Failed to serialize response"); } } -- cgit v1.2.3