diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-09-09 21:51:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-09 22:51:55 +0200 |
commit | 04a9cc95ac1349ab192c93c231c1ac97ee02fb00 (patch) | |
tree | 2b751448d8151442fc9909750077071fb58b5c3e /cli/main.rs | |
parent | d9b5bccdea01928eba5a4fcff1fa1486d428b2e8 (diff) |
feat: Add better error messages for unstable APIs (#25519)
This commit improves error messages for unstable APIs:
- `--unstable-broadcast-channel`
- `--unstable-cron`
- `--unstable-http`
- `--unstable-kv`
- `--unstable-temporal`
By providing information and hints what went wrong and how the
error can be fixed. It reuses the same infra that was added in
https://github.com/denoland/deno/pull/21764.
Diffstat (limited to 'cli/main.rs')
-rw-r--r-- | cli/main.rs | 68 |
1 files changed, 56 insertions, 12 deletions
diff --git a/cli/main.rs b/cli/main.rs index da274d8ed..c1f0c7910 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -337,17 +337,61 @@ fn exit_with_message(message: &str, code: i32) -> ! { std::process::exit(code); } -fn get_suggestions_for_commonjs_error(e: &JsError) -> Vec<FixSuggestion> { - if e.name.as_deref() == Some("ReferenceError") { - 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`."), - ]; - } +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("createHttpClient is not a function") { + return vec![ + FixSuggestion::info("Deno.createHttpClient() is an unstable API."), + FixSuggestion::hint( + "Run again with `--unstable-http` 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.", + ), + ]; } } @@ -359,7 +403,7 @@ fn exit_for_error(error: AnyError) -> ! { let mut error_code = 1; if let Some(e) = error.downcast_ref::<JsError>() { - let suggestions = get_suggestions_for_commonjs_error(e); + let suggestions = get_suggestions_for_terminal_errors(e); error_string = format_js_error_with_suggestions(e, suggestions); } else if let Some(SnapshotFromLockfileError::IntegrityCheckFailed(e)) = error.downcast_ref::<SnapshotFromLockfileError>() |