From 52459faf0b35ed157bb640d24c6107e1ff00aded Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Sat, 19 Mar 2022 15:59:44 +0100 Subject: [PATCH] fix(ops): throw TypeError on op return failure (#14033) Fixes #14028 --- core/runtime.rs | 20 ++++++++++++++++++++ ops/lib.rs | 9 +++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/runtime.rs b/core/runtime.rs index 6ca586997c..5b6ce39982 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -2854,4 +2854,24 @@ assertEquals(1, notify_return_value); assert_eq!(2, PROMISE_REJECT.load(Ordering::Relaxed)); assert_eq!(2, UNCAUGHT_EXCEPTION.load(Ordering::Relaxed)); } + + #[test] + fn test_op_return_serde_v8_error() { + #[op] + fn op_err() -> Result, anyhow::Error> { + Ok([(1, 2), (3, 4)].into_iter().collect()) // Maps can't have non-string keys in serde_v8 + } + + let ext = Extension::builder().ops(vec![op_err::decl()]).build(); + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], + ..Default::default() + }); + assert!(runtime + .execute_script( + "test_op_return_serde_v8_error.js", + "Deno.core.opSync('op_err')" + ) + .is_err()); + } } diff --git a/ops/lib.rs b/ops/lib.rs index 49057507d8..4a24b6e33c 100644 --- a/ops/lib.rs +++ b/ops/lib.rs @@ -260,8 +260,13 @@ fn codegen_sync_ret( quote! {} } else { quote! { - let ret = #core::serde_v8::to_v8(scope, v).unwrap(); - rv.set(ret); + match #core::serde_v8::to_v8(scope, v) { + Ok(ret) => rv.set(ret), + Err(err) => #core::_ops::throw_type_error( + scope, + format!("Error serializing return: {}", #core::anyhow::Error::from(err)), + ), + }; } };