Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: more specific error handling #512

Merged
merged 24 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
448 changes: 381 additions & 67 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ members = ["lib"]

[workspace.dependencies]
deno_unsync = { version = "0.4.0", default-features = false }
thiserror = "2"
deno_error = "0.5.1"

[lib]
name = "deno_graph"
Expand All @@ -38,15 +40,14 @@ path = "tests/ecosystem_test.rs"
harness = false

[dependencies]
anyhow = "1.0.43"
async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.43.2", features = ["dep_analysis", "emit"] }
deno_ast = { version = "0.44.0", features = ["dep_analysis", "emit"] }
deno_unsync.workspace = true
deno_semver = "0.5.13"
deno_semver = "0.6.0"
encoding_rs = "0.8.33"
futures = "0.3.26"
import_map = "0.20.0"
import_map = "0.21.0"
indexmap = { version = "2", features = ["serde"] }
log = "0.4.20"
monch = "0.5.0"
Expand All @@ -56,10 +57,11 @@ regex = "1.10.2"
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = { version = "1.0.67", features = ["preserve_order"] }
sha2 = "^0.10.0"
thiserror = "1.0.24"
thiserror.workspace = true
deno_error.workspace = true
twox-hash = { version = "1.6.3", optional = true }
url = { version = "2.2.2", features = ["serde"] }
wasm_dep_analyzer = "0.1.0"
url = { version = "2.5.3", features = ["serde"] }
wasm_dep_analyzer = "0.2.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
deno_unsync = { workspace = true, features = ["tokio"] }
Expand All @@ -71,7 +73,7 @@ pretty_assertions = "1.0.0"
reqwest = { version = "0.12.4", default-features = false, features = ["http2", "charset", "rustls-tls-webpki-roots"] }
tempfile = "3.4.0"
tokio = { version = "1.10.1", features = ["macros", "rt-multi-thread", "sync"] }
deno_terminal = "0.1.1"
deno_terminal = "0.2.0"
env_logger = "0.11.3"

[profile.release]
Expand Down
3 changes: 2 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0.43"
console_error_panic_hook = "0.1.7"
deno_graph = { path = "../", default-features = false }
getrandom = { version = "*", features = ["js"] }
Expand All @@ -25,6 +24,8 @@ serde_json = { version = "1.0.67", features = ["preserve_order"] }
serde-wasm-bindgen = "0.5.0"
wasm-bindgen = { version = "=0.2.91" }
wasm-bindgen-futures = { version = "=0.4.41" }
thiserror.workspace = true
deno_error.workspace = true

[dev-dependencies]
pretty_assertions = "1.0.0"
32 changes: 24 additions & 8 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#![allow(clippy::unused_unit)]
#![deny(clippy::disallowed_methods)]
#![deny(clippy::disallowed_types)]

use std::collections::HashMap;
#![deny(clippy::unnecessary_wraps)]

use deno_graph::resolve_import;
use deno_graph::source::load_data_url;
Expand All @@ -25,13 +24,28 @@ use deno_graph::ModuleSpecifier;
use deno_graph::Range;
use deno_graph::ReferrerImports;
use deno_graph::SpecifierError;
use std::collections::HashMap;
use std::sync::Arc;

use anyhow::anyhow;
use futures::future;
use serde::Deserialize;
use serde::Serialize;
use wasm_bindgen::prelude::*;

#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
pub enum WasmError {
#[error("load rejected or errored")]
Load,
#[error("JavaScript resolve threw.")]
JavaScriptResolve,
#[error("JavaScript resolveTypes() function threw.")]
JavaScriptResolveTypes,
#[error("{0}")]
// String because serde_wasm_bindgen::Error is not thread-safe.
Deserialize(String),
}

pub struct JsLoader {
load: js_sys::Function,
maybe_cache_info: Option<js_sys::Function>,
Expand Down Expand Up @@ -100,7 +114,7 @@ impl Loader for JsLoader {
};
response
.map(|value| serde_wasm_bindgen::from_value(value).unwrap())
.map_err(|_| anyhow!("load rejected or errored"))
.map_err(|_| Arc::new(WasmError::Load).into())
};
Box::pin(f)
}
Expand Down Expand Up @@ -168,11 +182,13 @@ impl Resolver for JsResolver {
let arg2 = JsValue::from(referrer_range.specifier.to_string());
let value = match resolve.call2(&this, &arg1, &arg2) {
Ok(value) => value,
Err(_) => return Err(anyhow!("JavaScript resolve threw.").into()),
Err(_) => return Err(Box::new(WasmError::JavaScriptResolve).into()),
};
let value: String = match serde_wasm_bindgen::from_value(value) {
Ok(value) => value,
Err(err) => return Err(anyhow!("{}", err.to_string()).into()),
Err(err) => {
return Err(Box::new(WasmError::Deserialize(err.to_string())).into())
}
};
ModuleSpecifier::parse(&value)
.map_err(|err| ResolveError::Specifier(SpecifierError::InvalidUrl(err)))
Expand All @@ -191,10 +207,10 @@ impl Resolver for JsResolver {
let arg1 = JsValue::from(specifier.to_string());
let value = resolve_types
.call1(&this, &arg1)
.map_err(|_| anyhow!("JavaScript resolveTypes() function threw."))?;
.map_err(|_| Box::new(WasmError::JavaScriptResolveTypes))?;
let result: Option<JsResolveTypesResponse> =
serde_wasm_bindgen::from_value(value)
.map_err(|err| anyhow!("{}", err.to_string()))?;
.map_err(|err| Box::new(WasmError::Deserialize(err.to_string())))?;
Ok(result.map(|v| (v.types, v.source)))
} else {
Ok(None)
Expand Down
36 changes: 15 additions & 21 deletions src/fast_check/transform_dts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,13 @@ impl FastCheckDtsDiagnostic {
}
}

pub fn range(&self) -> Option<&FastCheckDiagnosticRange> {
pub fn range(&self) -> &FastCheckDiagnosticRange {
match self {
FastCheckDtsDiagnostic::UnableToInferType { range } => Some(range),
FastCheckDtsDiagnostic::UnableToInferTypeFallbackAny { range } => {
Some(range)
}
FastCheckDtsDiagnostic::UnableToInferTypeFromProp { range } => {
Some(range)
}
FastCheckDtsDiagnostic::UnableToInferTypeFromSpread { range } => {
Some(range)
}
FastCheckDtsDiagnostic::UnsupportedUsing { range } => Some(range),
FastCheckDtsDiagnostic::UnableToInferType { range } => range,
FastCheckDtsDiagnostic::UnableToInferTypeFallbackAny { range } => range,
FastCheckDtsDiagnostic::UnableToInferTypeFromProp { range } => range,
FastCheckDtsDiagnostic::UnableToInferTypeFromSpread { range } => range,
FastCheckDtsDiagnostic::UnsupportedUsing { range } => range,
}
}
}
Expand Down Expand Up @@ -756,14 +750,14 @@ impl<'a> FastCheckDtsTransformer<'a> {
expr: Expr,
as_const: bool,
as_readonly: bool,
) -> Option<Box<TsTypeAnn>> {
) -> Box<TsTypeAnn> {
if let Some(ts_type) =
self.expr_to_ts_type(expr.clone(), as_const, as_readonly)
{
Some(type_ann(ts_type))
type_ann(ts_type)
} else {
self.mark_diagnostic_any_fallback(expr.range());
Some(any_type_ann())
any_type_ann()
}
}

Expand Down Expand Up @@ -915,35 +909,35 @@ impl<'a> FastCheckDtsTransformer<'a> {
match &mut *assign_pat.left {
Pat::Ident(ident) => {
if ident.type_ann.is_none() {
ident.type_ann = self.infer_expr_fallback_any(
ident.type_ann = Some(self.infer_expr_fallback_any(
*assign_pat.right.clone(),
false,
false,
);
));
}

ident.optional = true;
Some(Pat::Ident(ident.clone()))
}
Pat::Array(arr_pat) => {
if arr_pat.type_ann.is_none() {
arr_pat.type_ann = self.infer_expr_fallback_any(
arr_pat.type_ann = Some(self.infer_expr_fallback_any(
*assign_pat.right.clone(),
false,
false,
);
));
}

arr_pat.optional = true;
Some(Pat::Array(arr_pat.clone()))
}
Pat::Object(obj_pat) => {
if obj_pat.type_ann.is_none() {
obj_pat.type_ann = self.infer_expr_fallback_any(
obj_pat.type_ann = Some(self.infer_expr_fallback_any(
*assign_pat.right.clone(),
false,
false,
);
));
}

obj_pat.optional = true;
Expand Down
Loading