diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-02-23 14:51:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-23 14:51:29 -0500 |
| commit | 4e1abb4f3a1fbdac25b1e7db0588572e4d5a6579 (patch) | |
| tree | 644ace7dc1acac7b09bfab037e0ca589fa11987b /cli/ops/compiler.rs | |
| parent | 45eb2f9b37c2c7498c58eb45f76667aaa4a7d731 (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/compiler.rs')
| -rw-r--r-- | cli/ops/compiler.rs | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs index 4d67a8009..a559bb539 100644 --- a/cli/ops/compiler.rs +++ b/cli/ops/compiler.rs @@ -4,6 +4,7 @@ use super::dispatch_json::JsonOp; use super::dispatch_json::Value; use crate::futures::future::try_join_all; use crate::msg; +use crate::op_error::OpError; use crate::ops::json_op; use crate::state::State; use deno_core::Loader; @@ -38,7 +39,7 @@ fn op_cache( state: &State, args: Value, _zero_copy: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: CacheArgs = serde_json::from_value(args)?; let module_specifier = ModuleSpecifier::resolve_url(&args.module_id) @@ -66,7 +67,7 @@ fn op_resolve_modules( state: &State, args: Value, _data: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: SpecifiersReferrerArgs = serde_json::from_value(args)?; let (referrer, is_main) = if let Some(referrer) = args.referrer { (referrer, false) @@ -77,11 +78,10 @@ fn op_resolve_modules( let mut specifiers = vec![]; for specifier in &args.specifiers { - let resolved_specifier = state.resolve(specifier, &referrer, is_main); - match resolved_specifier { - Ok(ms) => specifiers.push(ms.as_str().to_owned()), - Err(err) => return Err(err), - } + let specifier = state + .resolve(specifier, &referrer, is_main) + .map_err(OpError::from)?; + specifiers.push(specifier.as_str().to_owned()); } Ok(JsonOp::Sync(json!(specifiers))) @@ -91,7 +91,7 @@ fn op_fetch_source_files( state: &State, args: Value, _data: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: SpecifiersReferrerArgs = serde_json::from_value(args)?; let ref_specifier = if let Some(referrer) = args.referrer { @@ -122,7 +122,7 @@ fn op_fetch_source_files( }) .collect(); - let files = try_join_all(file_futures).await?; + let files = try_join_all(file_futures).await.map_err(OpError::from)?; // We want to get an array of futures that resolves to let v = files.into_iter().map(|f| { async { @@ -134,7 +134,8 @@ fn op_fetch_source_files( global_state .file_fetcher .fetch_source_file_async(&types_specifier, ref_specifier.clone()) - .await? + .await + .map_err(OpError::from)? } _ => f, }; @@ -146,12 +147,13 @@ fn op_fetch_source_files( global_state .wasm_compiler .compile_async(global_state.clone(), &file) - .await? + .await + .map_err(|e| OpError::other(e.to_string()))? .code } _ => String::from_utf8(file.source_code).unwrap(), }; - Ok::<_, ErrBox>(json!({ + Ok::<_, OpError>(json!({ "url": file.url.to_string(), "filename": file.filename.to_str().unwrap(), "mediaType": file.media_type as i32, @@ -177,7 +179,7 @@ fn op_fetch_asset( _state: &State, args: Value, _data: Option<ZeroCopyBuf>, -) -> Result<JsonOp, ErrBox> { +) -> Result<JsonOp, OpError> { let args: FetchRemoteAssetArgs = serde_json::from_value(args)?; debug!("args.name: {}", args.name); |
