summaryrefslogtreecommitdiff
path: root/runtime/fmt_errors.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-10-15 17:59:28 +0100
committerGitHub <noreply@github.com>2024-10-15 18:59:28 +0200
commit38888061698f230f2288cf520daf86d781c395bd (patch)
tree0d6f22a437c8d00c58fe8d6770310ac598384bbc /runtime/fmt_errors.rs
parent9d93e333a6f6fe508cdf6c2fb8c91375048ec2a9 (diff)
refactor: always apply hint when formatting JsError (#26252)
Moves code for generating suggestions and hint to `cli/fmt_errors.rs`. This effectively applies suggestion to any place that format JS errors in terminal, like `deno test`. Addresses https://github.com/denoland/deno/pull/26218#issuecomment-2409139055
Diffstat (limited to 'runtime/fmt_errors.rs')
-rw-r--r--runtime/fmt_errors.rs105
1 files changed, 91 insertions, 14 deletions
diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs
index 44a947732..0d4274e8a 100644
--- a/runtime/fmt_errors.rs
+++ b/runtime/fmt_errors.rs
@@ -282,28 +282,105 @@ fn format_js_error_inner(
s
}
-/// Format a [`JsError`] for terminal output.
-pub fn format_js_error(js_error: &JsError) -> String {
- let circular =
- find_recursive_cause(js_error).map(|reference| IndexedErrorReference {
- reference,
- index: 1,
- });
+fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
+ if let Some(msg) = &e.message {
+ if msg.contains("module is not defined")
+ || msg.contains("exports is not defined")
+ {
+ return vec![
+ FixSuggestion::info(
+ "Deno does not support CommonJS modules without `.cjs` extension.",
+ ),
+ FixSuggestion::hint(
+ "Rewrite this module to ESM or change the file extension to `.cjs`.",
+ ),
+ ];
+ } else if msg.contains("openKv is not a function") {
+ return vec![
+ FixSuggestion::info("Deno.openKv() is an unstable API."),
+ FixSuggestion::hint(
+ "Run again with `--unstable-kv` flag to enable this API.",
+ ),
+ ];
+ } else if msg.contains("cron is not a function") {
+ return vec![
+ FixSuggestion::info("Deno.cron() is an unstable API."),
+ FixSuggestion::hint(
+ "Run again with `--unstable-cron` flag to enable this API.",
+ ),
+ ];
+ } else if msg.contains("WebSocketStream is not defined") {
+ return vec![
+ FixSuggestion::info("new WebSocketStream() is an unstable API."),
+ FixSuggestion::hint(
+ "Run again with `--unstable-net` flag to enable this API.",
+ ),
+ ];
+ } else if msg.contains("Temporal is not defined") {
+ return vec![
+ FixSuggestion::info("Temporal is an unstable API."),
+ FixSuggestion::hint(
+ "Run again with `--unstable-temporal` flag to enable this API.",
+ ),
+ ];
+ } else if msg.contains("BroadcastChannel is not defined") {
+ return vec![
+ FixSuggestion::info("BroadcastChannel is an unstable API."),
+ FixSuggestion::hint(
+ "Run again with `--unstable-broadcast-channel` flag to enable this API.",
+ ),
+ ];
+ } else if msg.contains("window is not defined") {
+ return vec![
+ FixSuggestion::info("window global is not available in Deno 2."),
+ FixSuggestion::hint("Replace `window` with `globalThis`."),
+ ];
+ } else if msg.contains("UnsafeWindowSurface is not a constructor") {
+ return vec![
+ FixSuggestion::info("Deno.UnsafeWindowSurface is an unstable API."),
+ FixSuggestion::hint(
+ "Run again with `--unstable-webgpu` flag to enable this API.",
+ ),
+ ];
+ // Try to capture errors like:
+ // ```
+ // Uncaught Error: Cannot find module '../build/Release/canvas.node'
+ // Require stack:
+ // - /.../deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js
+ // - /.../.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/canvas.js
+ // ```
+ } else if msg.contains("Cannot find module")
+ && msg.contains("Require stack")
+ && msg.contains(".node'")
+ {
+ return vec![
+ FixSuggestion::info_multiline(
+ &[
+ "Trying to execute an npm package using Node-API addons,",
+ "these packages require local `node_modules` directory to be present."
+ ]
+ ),
+ FixSuggestion::hint_multiline(
+ &[
+ "Add `\"nodeModulesDir\": \"auto\" option to `deno.json`, and then run",
+ "`deno install --allow-scripts=npm:<package> --entrypoint <script>` to setup `node_modules` directory."
+ ]
+ )
+ ];
+ }
+ }
- format_js_error_inner(js_error, circular, true, vec![])
+ vec![]
}
-/// Format a [`JsError`] for terminal output, printing additional suggestions.
-pub fn format_js_error_with_suggestions(
- js_error: &JsError,
- suggestions: Vec<FixSuggestion>,
-) -> String {
+/// Format a [`JsError`] for terminal output.
+pub fn format_js_error(js_error: &JsError) -> String {
let circular =
find_recursive_cause(js_error).map(|reference| IndexedErrorReference {
reference,
index: 1,
});
-
+ let suggestions = get_suggestions_for_terminal_errors(js_error);
format_js_error_inner(js_error, circular, true, suggestions)
}