diff options
Diffstat (limited to 'cli/ops/dispatch_json.rs')
-rw-r--r-- | cli/ops/dispatch_json.rs | 53 |
1 files changed, 34 insertions, 19 deletions
diff --git a/cli/ops/dispatch_json.rs b/cli/ops/dispatch_json.rs index 5ed7e6c6d..6e0eddac8 100644 --- a/cli/ops/dispatch_json.rs +++ b/cli/ops/dispatch_json.rs @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::op_error::OpError; use deno_core::Buf; use deno_core::CoreIsolateState; +use deno_core::ErrBox; use deno_core::Op; use deno_core::ZeroCopyBuf; use futures::future::FutureExt; @@ -11,7 +11,7 @@ pub use serde_json::Value; use std::future::Future; use std::pin::Pin; -pub type JsonResult = Result<Value, OpError>; +pub type JsonResult = Result<Value, ErrBox>; pub type AsyncJsonOp = Pin<Box<dyn Future<Output = JsonResult>>>; @@ -23,17 +23,18 @@ pub enum JsonOp { AsyncUnref(AsyncJsonOp), } -fn json_err(err: OpError) -> Value { - json!({ - "message": err.msg, - "kind": err.kind_str, - }) -} - -pub fn serialize_result(promise_id: Option<u64>, result: JsonResult) -> Buf { +pub fn serialize_result( + rust_err_to_json_fn: &'static dyn deno_core::RustErrToJsonFn, + promise_id: Option<u64>, + result: JsonResult, +) -> Buf { let value = match result { Ok(v) => json!({ "ok": v, "promiseId": promise_id }), - Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }), + Err(err) => { + let serialized_err = (&rust_err_to_json_fn)(&err); + let err_value: Value = serde_json::from_slice(&serialized_err).unwrap(); + json!({ "err": err_value, "promiseId": promise_id }) + } }; serde_json::to_vec(&value).unwrap().into_boxed_slice() } @@ -52,14 +53,15 @@ where &mut CoreIsolateState, Value, &mut [ZeroCopyBuf], - ) -> Result<JsonOp, OpError>, + ) -> Result<JsonOp, ErrBox>, { move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| { assert!(!zero_copy.is_empty(), "Expected JSON string at position 0"); + let rust_err_to_json_fn = isolate_state.rust_err_to_json_fn; let async_args: AsyncArgs = match serde_json::from_slice(&zero_copy[0]) { Ok(args) => args, Err(e) => { - let buf = serialize_result(None, Err(OpError::from(e))); + let buf = serialize_result(rust_err_to_json_fn, None, Err(e.into())); return Op::Sync(buf); } }; @@ -67,31 +69,44 @@ where let is_sync = promise_id.is_none(); let result = serde_json::from_slice(&zero_copy[0]) - .map_err(OpError::from) + .map_err(ErrBox::from) .and_then(|args| d(isolate_state, args, &mut zero_copy[1..])); // Convert to Op match result { Ok(JsonOp::Sync(sync_value)) => { assert!(promise_id.is_none()); - Op::Sync(serialize_result(promise_id, Ok(sync_value))) + Op::Sync(serialize_result( + rust_err_to_json_fn, + promise_id, + Ok(sync_value), + )) } Ok(JsonOp::Async(fut)) => { assert!(promise_id.is_some()); let fut2 = fut.then(move |result| { - futures::future::ready(serialize_result(promise_id, result)) + futures::future::ready(serialize_result( + rust_err_to_json_fn, + promise_id, + result, + )) }); Op::Async(fut2.boxed_local()) } Ok(JsonOp::AsyncUnref(fut)) => { assert!(promise_id.is_some()); let fut2 = fut.then(move |result| { - futures::future::ready(serialize_result(promise_id, result)) + futures::future::ready(serialize_result( + rust_err_to_json_fn, + promise_id, + result, + )) }); Op::AsyncUnref(fut2.boxed_local()) } Err(sync_err) => { - let buf = serialize_result(promise_id, Err(sync_err)); + let buf = + serialize_result(rust_err_to_json_fn, promise_id, Err(sync_err)); if is_sync { Op::Sync(buf) } else { @@ -102,7 +117,7 @@ where } } -pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, OpError> +pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, ErrBox> where F: 'static + Send + FnOnce() -> JsonResult, { |