summaryrefslogtreecommitdiff
path: root/cli/ops/dispatch_json.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-02-23 14:51:29 -0500
committerGitHub <noreply@github.com>2020-02-23 14:51:29 -0500
commit4e1abb4f3a1fbdac25b1e7db0588572e4d5a6579 (patch)
tree644ace7dc1acac7b09bfab037e0ca589fa11987b /cli/ops/dispatch_json.rs
parent45eb2f9b37c2c7498c58eb45f76667aaa4a7d731 (diff)
refactor: use OpError instead of ErrBox for errors in ops (#4058)
To better reflect changes in error types in JS from #3662 this PR changes default error type used in ops from "ErrBox" to "OpError". "OpError" is a type that can be sent over to JSON; it has all information needed to construct error in JavaScript. That made "GetErrorKind" trait useless and so it was removed altogether. To provide compatibility with previous use of "ErrBox" an implementation of "From<ErrBox> for OpError" was added, however, it is an escape hatch and ops implementors should strive to use "OpError" directly.
Diffstat (limited to 'cli/ops/dispatch_json.rs')
-rw-r--r--cli/ops/dispatch_json.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/cli/ops/dispatch_json.rs b/cli/ops/dispatch_json.rs
index 2cb3a8400..70498eb8f 100644
--- a/cli/ops/dispatch_json.rs
+++ b/cli/ops/dispatch_json.rs
@@ -1,4 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use crate::op_error::OpError;
use deno_core::*;
use futures::future::FutureExt;
pub use serde_derive::Deserialize;
@@ -7,7 +8,7 @@ pub use serde_json::Value;
use std::future::Future;
use std::pin::Pin;
-pub type JsonResult = Result<Value, ErrBox>;
+pub type JsonResult = Result<Value, OpError>;
pub type AsyncJsonOp = Pin<Box<dyn Future<Output = JsonResult>>>;
@@ -19,11 +20,10 @@ pub enum JsonOp {
AsyncUnref(AsyncJsonOp),
}
-fn json_err(err: ErrBox) -> Value {
- use crate::deno_error::GetErrorKind;
+fn json_err(err: OpError) -> Value {
json!({
- "message": err.to_string(),
- "kind": err.kind() as u32,
+ "message": err.msg,
+ "kind": err.kind as u32,
})
}
@@ -43,13 +43,13 @@ struct AsyncArgs {
pub fn json_op<D>(d: D) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp
where
- D: Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, ErrBox>,
+ D: Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>,
{
move |control: &[u8], zero_copy: Option<ZeroCopyBuf>| {
let async_args: AsyncArgs = match serde_json::from_slice(control) {
Ok(args) => args,
Err(e) => {
- let buf = serialize_result(None, Err(ErrBox::from(e)));
+ let buf = serialize_result(None, Err(OpError::from(e)));
return CoreOp::Sync(buf);
}
};
@@ -57,7 +57,7 @@ where
let is_sync = promise_id.is_none();
let result = serde_json::from_slice(control)
- .map_err(ErrBox::from)
+ .map_err(OpError::from)
.and_then(|args| d(args, zero_copy));
// Convert to CoreOp
@@ -92,7 +92,7 @@ where
}
}
-pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, ErrBox>
+pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, OpError>
where
F: 'static + Send + FnOnce() -> JsonResult,
{