2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-08-25 18:22:15 -04:00
|
|
|
|
|
|
|
//! There are many types of errors in Deno:
|
2020-09-14 12:48:57 -04:00
|
|
|
//! - AnyError: a generic wrapper that can encapsulate any type of error.
|
2020-09-06 15:44:29 -04:00
|
|
|
//! - JsError: a container for the error message and stack trace for exceptions
|
2020-08-25 18:22:15 -04:00
|
|
|
//! thrown in JavaScript code. We use this to pretty-print stack traces.
|
|
|
|
//! - Diagnostic: these are errors that originate in TypeScript's compiler.
|
2020-09-14 12:48:57 -04:00
|
|
|
//! They're similar to JsError, in that they have line numbers. But
|
|
|
|
//! Diagnostics are compile-time type errors, whereas JsErrors are runtime
|
2020-08-25 18:22:15 -04:00
|
|
|
//! exceptions.
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::Diagnostic;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_graph::ModuleGraphError;
|
|
|
|
use deno_graph::ResolutionError;
|
2021-09-10 21:38:24 -04:00
|
|
|
use import_map::ImportMapError;
|
2020-08-25 18:22:15 -04:00
|
|
|
|
|
|
|
fn get_import_map_error_class(_: &ImportMapError) -> &'static str {
|
|
|
|
"URIError"
|
|
|
|
}
|
|
|
|
|
2021-06-11 09:03:42 -04:00
|
|
|
fn get_diagnostic_class(_: &Diagnostic) -> &'static str {
|
2020-08-25 18:22:15 -04:00
|
|
|
"SyntaxError"
|
|
|
|
}
|
|
|
|
|
2022-11-25 18:29:48 -05:00
|
|
|
fn get_module_graph_error_class(err: &ModuleGraphError) -> &'static str {
|
2021-10-10 17:26:22 -04:00
|
|
|
match err {
|
2023-02-09 22:00:23 -05:00
|
|
|
ModuleGraphError::LoadingErr(_, _, err) => {
|
|
|
|
get_error_class_name(err.as_ref())
|
|
|
|
}
|
2022-12-06 14:12:51 -05:00
|
|
|
ModuleGraphError::InvalidTypeAssertion { .. } => "SyntaxError",
|
2021-10-10 17:26:22 -04:00
|
|
|
ModuleGraphError::ParseErr(_, diagnostic) => {
|
|
|
|
get_diagnostic_class(diagnostic)
|
|
|
|
}
|
|
|
|
ModuleGraphError::ResolutionError(err) => get_resolution_error_class(err),
|
2023-02-09 22:00:23 -05:00
|
|
|
ModuleGraphError::UnsupportedMediaType { .. }
|
|
|
|
| ModuleGraphError::UnsupportedImportAssertionType { .. } => "TypeError",
|
2023-02-15 11:30:54 -05:00
|
|
|
ModuleGraphError::Missing(_, _)
|
|
|
|
| ModuleGraphError::MissingDynamic(_, _) => "NotFound",
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_resolution_error_class(err: &ResolutionError) -> &'static str {
|
|
|
|
match err {
|
2022-01-31 17:33:57 -05:00
|
|
|
ResolutionError::ResolverError { error, .. } => {
|
|
|
|
get_error_class_name(error.as_ref())
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
_ => "TypeError",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn get_error_class_name(e: &AnyError) -> &'static str {
|
2020-12-13 13:45:53 -05:00
|
|
|
deno_runtime::errors::get_error_class_name(e)
|
2020-09-14 12:48:57 -04:00
|
|
|
.or_else(|| {
|
|
|
|
e.downcast_ref::<ImportMapError>()
|
|
|
|
.map(get_import_map_error_class)
|
|
|
|
})
|
2021-06-11 09:03:42 -04:00
|
|
|
.or_else(|| e.downcast_ref::<Diagnostic>().map(get_diagnostic_class))
|
2021-10-10 17:26:22 -04:00
|
|
|
.or_else(|| {
|
|
|
|
e.downcast_ref::<ModuleGraphError>()
|
|
|
|
.map(get_module_graph_error_class)
|
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
e.downcast_ref::<ResolutionError>()
|
|
|
|
.map(get_resolution_error_class)
|
|
|
|
})
|
2020-09-14 12:48:57 -04:00
|
|
|
.unwrap_or_else(|| {
|
2023-02-15 13:20:40 -05:00
|
|
|
if cfg!(debug) {
|
|
|
|
log::warn!(
|
|
|
|
"Error '{}' contains boxed error of unknown type:{}",
|
|
|
|
e,
|
|
|
|
e.chain().map(|e| format!("\n {e:?}")).collect::<String>()
|
|
|
|
);
|
|
|
|
}
|
2021-11-07 09:33:56 -05:00
|
|
|
"Error"
|
2020-09-14 12:48:57 -04:00
|
|
|
})
|
2020-08-25 18:22:15 -04:00
|
|
|
}
|