summaryrefslogtreecommitdiff
path: root/cli/ops/files.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/files.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/files.rs')
-rw-r--r--cli/ops/files.rs33
1 files changed, 14 insertions, 19 deletions
diff --git a/cli/ops/files.rs b/cli/ops/files.rs
index 9619051a7..2fc41bc34 100644
--- a/cli/ops/files.rs
+++ b/cli/ops/files.rs
@@ -1,10 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use super::io::StreamResource;
-use crate::deno_error::bad_resource;
-use crate::deno_error::DenoError;
-use crate::deno_error::ErrorKind;
use crate::fs as deno_fs;
+use crate::op_error::OpError;
use crate::ops::json_op;
use crate::state::State;
use deno_core::*;
@@ -46,7 +44,7 @@ fn op_open(
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
-) -> Result<JsonOp, ErrBox> {
+) -> Result<JsonOp, OpError> {
let args: OpenArgs = serde_json::from_value(args)?;
let filename = deno_fs::resolve_from_cwd(Path::new(&args.filename))?;
let state_ = state.clone();
@@ -113,17 +111,14 @@ fn op_open(
open_options.create_new(true).read(true).write(true);
}
&_ => {
- return Err(ErrBox::from(DenoError::new(
- ErrorKind::Other,
- "Unknown open mode.".to_string(),
- )));
+ // TODO: this should be type error
+ return Err(OpError::other("Unknown open mode.".to_string()));
}
}
} else {
- return Err(ErrBox::from(DenoError::new(
- ErrorKind::Other,
+ return Err(OpError::other(
"Open requires either mode or options.".to_string(),
- )));
+ ));
};
let is_sync = args.promise_id.is_none();
@@ -154,14 +149,14 @@ fn op_close(
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
-) -> Result<JsonOp, ErrBox> {
+) -> Result<JsonOp, OpError> {
let args: CloseArgs = serde_json::from_value(args)?;
let mut state = state.borrow_mut();
state
.resource_table
.close(args.rid as u32)
- .ok_or_else(bad_resource)?;
+ .ok_or_else(OpError::bad_resource)?;
Ok(JsonOp::Sync(json!({})))
}
@@ -178,7 +173,7 @@ fn op_seek(
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
-) -> Result<JsonOp, ErrBox> {
+) -> Result<JsonOp, OpError> {
let args: SeekArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let offset = args.offset;
@@ -189,9 +184,9 @@ fn op_seek(
1 => SeekFrom::Current(i64::from(offset)),
2 => SeekFrom::End(i64::from(offset)),
_ => {
- return Err(ErrBox::from(DenoError::new(
- ErrorKind::TypeError,
- format!("Invalid seek mode: {}", whence),
+ return Err(OpError::type_error(format!(
+ "Invalid seek mode: {}",
+ whence
)));
}
};
@@ -200,11 +195,11 @@ fn op_seek(
let resource = state
.resource_table
.get::<StreamResource>(rid)
- .ok_or_else(bad_resource)?;
+ .ok_or_else(OpError::bad_resource)?;
let tokio_file = match resource {
StreamResource::FsFile(ref file) => file,
- _ => return Err(bad_resource()),
+ _ => return Err(OpError::bad_resource()),
};
let mut file = futures::executor::block_on(tokio_file.try_clone())?;