diff --git a/core/core.js b/core/core.js index 7c39c1baed..f94ec8139c 100644 --- a/core/core.js +++ b/core/core.js @@ -44,18 +44,23 @@ } function processResponse(res) { - // const [ok, err] = res; - if (res[1] === null) { - return res[0]; + if (!isErr(res)) { + return res; } - throw processErr(res[1]); + throw processErr(res); + } + + // .$err_class_name is a special key that should only exist on errors + function isErr(res) { + return !!(res && res.$err_class_name); } function processErr(err) { - const [ErrorClass, args] = getErrorClassAndArgs(err.className); + const className = err.$err_class_name; + const [ErrorClass, args] = getErrorClassAndArgs(className); if (!ErrorClass) { return new Error( - `Unregistered error class: "${err.className}"\n ${err.message}\n Classes of errors returned from ops should be registered via Deno.core.registerErrorClass().`, + `Unregistered error class: "${className}"\n ${err.message}\n Classes of errors returned from ops should be registered via Deno.core.registerErrorClass().`, ); } return new ErrorClass(err.message, ...args); @@ -82,13 +87,12 @@ } function opAsyncHandler(promiseId, res) { - // const [ok, err] = res; const promise = promiseTable.get(promiseId); promiseTable.delete(promiseId); - if (!res[1]) { - promise.resolve(res[0]); + if (!isErr(res)) { + promise.resolve(res); } else { - promise.reject(processErr(res[1])); + promise.reject(processErr(res)); } } diff --git a/core/ops.rs b/core/ops.rs index 3af60d072c..867bb52105 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -74,11 +74,16 @@ pub enum Op { } #[derive(Serialize)] -pub struct OpResult(Option, Option); +#[serde(untagged)] +pub enum OpResult { + Ok(R), + Err(OpError), +} #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct OpError { + #[serde(rename = "$err_class_name")] class_name: &'static str, message: String, } @@ -88,14 +93,11 @@ pub fn serialize_op_result( state: Rc>, ) -> OpResponse { OpResponse::Value(Box::new(match result { - Ok(v) => OpResult::(Some(v), None), - Err(err) => OpResult::( - None, - Some(OpError { - class_name: (state.borrow().get_error_class_fn)(&err), - message: err.to_string(), - }), - ), + Ok(v) => OpResult::Ok(v), + Err(err) => OpResult::Err(OpError { + class_name: (state.borrow().get_error_class_fn)(&err), + message: err.to_string(), + }), })) }