Skip to content

Commit 6dd8307

Browse files
crowlKatsdsherret
andauthored
refactor: more specific error handling (#512)
Co-authored-by: David Sherret <[email protected]>
1 parent 9d7d799 commit 6dd8307

File tree

12 files changed

+287
-164
lines changed

12 files changed

+287
-164
lines changed

Cargo.lock

+30-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ members = ["lib"]
1414

1515
[workspace.dependencies]
1616
deno_unsync = { version = "0.4.0", default-features = false }
17+
thiserror = "2"
18+
deno_error = "0.5.3"
1719
sys_traits = "0.1.0"
1820

1921
[lib]
@@ -39,7 +41,6 @@ path = "tests/ecosystem_test.rs"
3941
harness = false
4042

4143
[dependencies]
42-
anyhow = "1.0.43"
4344
async-trait = "0.1.68"
4445
capacity_builder = "0.5.0"
4546
data-url = "0.3.0"
@@ -50,7 +51,7 @@ deno_path_util = "0.3.0"
5051
deno_semver = "0.7.1"
5152
encoding_rs = "0.8.33"
5253
futures = "0.3.26"
53-
import_map = "0.20.0"
54+
import_map = "0.21.0"
5455
indexmap = { version = "2", features = ["serde"] }
5556
log = "0.4.20"
5657
monch = "0.5.0"
@@ -61,10 +62,11 @@ serde = { version = "1.0.130", features = ["derive", "rc"] }
6162
serde_json = { version = "1.0.67", features = ["preserve_order"] }
6263
sha2 = "^0.10.0"
6364
sys_traits.workspace = true
64-
thiserror = "2.0.3"
65+
thiserror.workspace = true
66+
deno_error.workspace = true
6567
twox-hash = { version = "1.6.3", optional = true }
66-
url = { version = "2.2.2", features = ["serde"] }
67-
wasm_dep_analyzer = "0.1.0"
68+
url = { version = "2.5.3", features = ["serde"] }
69+
wasm_dep_analyzer = "0.2.0"
6870

6971
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
7072
deno_unsync = { workspace = true, features = ["tokio"] }
@@ -76,7 +78,7 @@ pretty_assertions = "1.0.0"
7678
reqwest = { version = "0.12.4", default-features = false, features = ["http2", "charset", "rustls-tls-webpki-roots"] }
7779
tempfile = "3.4.0"
7880
tokio = { version = "1.10.1", features = ["macros", "rt-multi-thread", "sync"] }
79-
deno_terminal = "0.1.1"
81+
deno_terminal = "0.2.0"
8082
env_logger = "0.11.3"
8183
sys_traits = { workspace = true, features = ["memory"] }
8284

lib/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ path = "lib.rs"
1414
crate-type = ["cdylib"]
1515

1616
[dependencies]
17-
anyhow = "1.0.43"
1817
console_error_panic_hook = "0.1.7"
1918
deno_graph = { path = "../", default-features = false }
2019
getrandom = { version = "*", features = ["js"] }
@@ -26,6 +25,8 @@ serde-wasm-bindgen = "0.5.0"
2625
sys_traits = { workspace = true, features = ["real", "wasm"] }
2726
wasm-bindgen = { version = "=0.2.91" }
2827
wasm-bindgen-futures = { version = "=0.4.41" }
28+
thiserror.workspace = true
29+
deno_error.workspace = true
2930

3031
[dev-dependencies]
3132
pretty_assertions = "1.0.0"

lib/lib.rs

+41-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#![allow(clippy::unused_unit)]
55
#![deny(clippy::disallowed_methods)]
66
#![deny(clippy::disallowed_types)]
7+
#![deny(clippy::unnecessary_wraps)]
78

8-
use std::collections::HashMap;
9-
9+
use deno_error::JsErrorBox;
1010
use deno_graph::resolve_import;
1111
use deno_graph::source::load_data_url;
1212
use deno_graph::source::CacheInfo;
@@ -25,13 +25,28 @@ use deno_graph::ModuleSpecifier;
2525
use deno_graph::Range;
2626
use deno_graph::ReferrerImports;
2727
use deno_graph::SpecifierError;
28+
use std::collections::HashMap;
29+
use std::sync::Arc;
2830

29-
use anyhow::anyhow;
3031
use futures::future;
3132
use serde::Deserialize;
3233
use serde::Serialize;
3334
use wasm_bindgen::prelude::*;
3435

36+
#[derive(Debug, thiserror::Error, deno_error::JsError)]
37+
#[class(generic)]
38+
pub enum WasmError {
39+
#[error("load rejected or errored")]
40+
Load,
41+
#[error("JavaScript resolve threw.")]
42+
JavaScriptResolve,
43+
#[error("JavaScript resolveTypes() function threw.")]
44+
JavaScriptResolveTypes,
45+
#[error("{0}")]
46+
// String because serde_wasm_bindgen::Error is not thread-safe.
47+
Deserialize(String),
48+
}
49+
3550
pub struct JsLoader {
3651
load: js_sys::Function,
3752
maybe_cache_info: Option<js_sys::Function>,
@@ -76,7 +91,11 @@ impl Loader for JsLoader {
7691
}
7792

7893
if specifier.scheme() == "data" {
79-
Box::pin(future::ready(load_data_url(specifier)))
94+
Box::pin(future::ready(load_data_url(specifier).map_err(|err| {
95+
deno_graph::source::LoadError::Other(Arc::new(JsErrorBox::from_err(
96+
err,
97+
)))
98+
})))
8099
} else {
81100
let specifier = specifier.clone();
82101
let context = JsValue::null();
@@ -100,7 +119,11 @@ impl Loader for JsLoader {
100119
};
101120
response
102121
.map(|value| serde_wasm_bindgen::from_value(value).unwrap())
103-
.map_err(|_| anyhow!("load rejected or errored"))
122+
.map_err(|_| {
123+
deno_graph::source::LoadError::Other(Arc::new(
124+
JsErrorBox::from_err(WasmError::Load),
125+
))
126+
})
104127
};
105128
Box::pin(f)
106129
}
@@ -168,11 +191,18 @@ impl Resolver for JsResolver {
168191
let arg2 = JsValue::from(referrer_range.specifier.to_string());
169192
let value = match resolve.call2(&this, &arg1, &arg2) {
170193
Ok(value) => value,
171-
Err(_) => return Err(anyhow!("JavaScript resolve threw.").into()),
194+
Err(_) => {
195+
return Err(JsErrorBox::from_err(WasmError::JavaScriptResolve).into())
196+
}
172197
};
173198
let value: String = match serde_wasm_bindgen::from_value(value) {
174199
Ok(value) => value,
175-
Err(err) => return Err(anyhow!("{}", err.to_string()).into()),
200+
Err(err) => {
201+
return Err(
202+
JsErrorBox::from_err(WasmError::Deserialize(err.to_string()))
203+
.into(),
204+
)
205+
}
176206
};
177207
ModuleSpecifier::parse(&value)
178208
.map_err(|err| ResolveError::Specifier(SpecifierError::InvalidUrl(err)))
@@ -191,10 +221,11 @@ impl Resolver for JsResolver {
191221
let arg1 = JsValue::from(specifier.to_string());
192222
let value = resolve_types
193223
.call1(&this, &arg1)
194-
.map_err(|_| anyhow!("JavaScript resolveTypes() function threw."))?;
224+
.map_err(|_| JsErrorBox::from_err(WasmError::JavaScriptResolveTypes))?;
195225
let result: Option<JsResolveTypesResponse> =
196-
serde_wasm_bindgen::from_value(value)
197-
.map_err(|err| anyhow!("{}", err.to_string()))?;
226+
serde_wasm_bindgen::from_value(value).map_err(|err| {
227+
JsErrorBox::from_err(WasmError::Deserialize(err.to_string()))
228+
})?;
198229
Ok(result.map(|v| (v.types, v.source)))
199230
} else {
200231
Ok(None)

src/fast_check/transform_dts.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,13 @@ impl FastCheckDtsDiagnostic {
4747
}
4848
}
4949

50-
pub fn range(&self) -> Option<&FastCheckDiagnosticRange> {
50+
pub fn range(&self) -> &FastCheckDiagnosticRange {
5151
match self {
52-
FastCheckDtsDiagnostic::UnableToInferType { range } => Some(range),
53-
FastCheckDtsDiagnostic::UnableToInferTypeFallbackAny { range } => {
54-
Some(range)
55-
}
56-
FastCheckDtsDiagnostic::UnableToInferTypeFromProp { range } => {
57-
Some(range)
58-
}
59-
FastCheckDtsDiagnostic::UnableToInferTypeFromSpread { range } => {
60-
Some(range)
61-
}
62-
FastCheckDtsDiagnostic::UnsupportedUsing { range } => Some(range),
52+
FastCheckDtsDiagnostic::UnableToInferType { range } => range,
53+
FastCheckDtsDiagnostic::UnableToInferTypeFallbackAny { range } => range,
54+
FastCheckDtsDiagnostic::UnableToInferTypeFromProp { range } => range,
55+
FastCheckDtsDiagnostic::UnableToInferTypeFromSpread { range } => range,
56+
FastCheckDtsDiagnostic::UnsupportedUsing { range } => range,
6357
}
6458
}
6559
}
@@ -756,14 +750,14 @@ impl<'a> FastCheckDtsTransformer<'a> {
756750
expr: Expr,
757751
as_const: bool,
758752
as_readonly: bool,
759-
) -> Option<Box<TsTypeAnn>> {
753+
) -> Box<TsTypeAnn> {
760754
if let Some(ts_type) =
761755
self.expr_to_ts_type(expr.clone(), as_const, as_readonly)
762756
{
763-
Some(type_ann(ts_type))
757+
type_ann(ts_type)
764758
} else {
765759
self.mark_diagnostic_any_fallback(expr.range());
766-
Some(any_type_ann())
760+
any_type_ann()
767761
}
768762
}
769763

@@ -915,35 +909,35 @@ impl<'a> FastCheckDtsTransformer<'a> {
915909
match &mut *assign_pat.left {
916910
Pat::Ident(ident) => {
917911
if ident.type_ann.is_none() {
918-
ident.type_ann = self.infer_expr_fallback_any(
912+
ident.type_ann = Some(self.infer_expr_fallback_any(
919913
*assign_pat.right.clone(),
920914
false,
921915
false,
922-
);
916+
));
923917
}
924918

925919
ident.optional = true;
926920
Some(Pat::Ident(ident.clone()))
927921
}
928922
Pat::Array(arr_pat) => {
929923
if arr_pat.type_ann.is_none() {
930-
arr_pat.type_ann = self.infer_expr_fallback_any(
924+
arr_pat.type_ann = Some(self.infer_expr_fallback_any(
931925
*assign_pat.right.clone(),
932926
false,
933927
false,
934-
);
928+
));
935929
}
936930

937931
arr_pat.optional = true;
938932
Some(Pat::Array(arr_pat.clone()))
939933
}
940934
Pat::Object(obj_pat) => {
941935
if obj_pat.type_ann.is_none() {
942-
obj_pat.type_ann = self.infer_expr_fallback_any(
936+
obj_pat.type_ann = Some(self.infer_expr_fallback_any(
943937
*assign_pat.right.clone(),
944938
false,
945939
false,
946-
);
940+
));
947941
}
948942

949943
obj_pat.optional = true;

0 commit comments

Comments
 (0)