summaryrefslogtreecommitdiff
path: root/cli/ops/fetch.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/fetch.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/fetch.rs')
-rw-r--r--cli/ops/fetch.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/cli/ops/fetch.rs b/cli/ops/fetch.rs
index 9758732c7..efcadc6a0 100644
--- a/cli/ops/fetch.rs
+++ b/cli/ops/fetch.rs
@@ -1,9 +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::DenoError;
-use crate::deno_error::ErrorKind;
use crate::http_util::{create_http_client, HttpBody};
+use crate::op_error::OpError;
use crate::ops::json_op;
use crate::state::State;
use deno_core::*;
@@ -29,7 +28,7 @@ pub fn op_fetch(
state: &State,
args: Value,
data: Option<ZeroCopyBuf>,
-) -> Result<JsonOp, ErrBox> {
+) -> Result<JsonOp, OpError> {
let args: FetchArgs = serde_json::from_value(args)?;
let url = args.url;
@@ -37,22 +36,20 @@ pub fn op_fetch(
create_http_client(state.borrow().global_state.flags.ca_file.clone())?;
let method = match args.method {
- Some(method_str) => Method::from_bytes(method_str.as_bytes())?,
+ Some(method_str) => Method::from_bytes(method_str.as_bytes())
+ .map_err(|e| OpError::other(e.to_string()))?,
None => Method::GET,
};
- let url_ = url::Url::parse(&url).map_err(ErrBox::from)?;
+ let url_ = url::Url::parse(&url).map_err(OpError::from)?;
// Check scheme before asking for net permission
let scheme = url_.scheme();
if scheme != "http" && scheme != "https" {
- return Err(
- DenoError::new(
- ErrorKind::TypeError,
- format!("scheme '{}' not supported", scheme),
- )
- .into(),
- );
+ return Err(OpError::type_error(format!(
+ "scheme '{}' not supported",
+ scheme
+ )));
}
state.check_net_url(&url_)?;