2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-08-25 22:22:15 +00:00
|
|
|
|
|
|
|
//! There are many types of errors in Deno:
|
2020-09-14 16:48:57 +00:00
|
|
|
//! - AnyError: a generic wrapper that can encapsulate any type of error.
|
2020-09-06 19:44:29 +00:00
|
|
|
//! - JsError: a container for the error message and stack trace for exceptions
|
2020-08-25 22:22:15 +00: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 16:48:57 +00: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 22:22:15 +00:00
|
|
|
//! exceptions.
|
|
|
|
|
2021-09-07 14:39:32 +00:00
|
|
|
use deno_ast::Diagnostic;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2023-03-21 15:46:40 +00:00
|
|
|
use deno_graph::ModuleError;
|
2021-10-10 21:26:22 +00:00
|
|
|
use deno_graph::ModuleGraphError;
|
|
|
|
use deno_graph::ResolutionError;
|
2021-09-11 01:38:24 +00:00
|
|
|
use import_map::ImportMapError;
|
2020-08-25 22:22:15 +00:00
|
|
|
|
|
|
|
fn get_import_map_error_class(_: &ImportMapError) -> &'static str {
|
|
|
|
"URIError"
|
|
|
|
}
|
|
|
|
|
2021-06-11 13:03:42 +00:00
|
|
|
fn get_diagnostic_class(_: &Diagnostic) -> &'static str {
|
2020-08-25 22:22:15 +00:00
|
|
|
"SyntaxError"
|
|
|
|
}
|
|
|
|
|
2022-11-25 23:29:48 +00:00
|
|
|
fn get_module_graph_error_class(err: &ModuleGraphError) -> &'static str {
|
2021-10-10 21:26:22 +00:00
|
|
|
match err {
|
2023-03-21 15:46:40 +00:00
|
|
|
ModuleGraphError::ModuleError(err) => match err {
|
|
|
|
ModuleError::LoadingErr(_, _, err) => get_error_class_name(err.as_ref()),
|
|
|
|
ModuleError::InvalidTypeAssertion { .. } => "SyntaxError",
|
|
|
|
ModuleError::ParseErr(_, diagnostic) => get_diagnostic_class(diagnostic),
|
|
|
|
ModuleError::UnsupportedMediaType { .. }
|
2023-09-07 13:09:16 +00:00
|
|
|
| ModuleError::UnsupportedImportAttributeType { .. } => "TypeError",
|
|
|
|
ModuleError::Missing(_, _)
|
|
|
|
| ModuleError::MissingDynamic(_, _)
|
|
|
|
| ModuleError::UnknownPackage { .. }
|
|
|
|
| ModuleError::UnknownPackageReq { .. } => "NotFound",
|
2023-03-21 15:46:40 +00:00
|
|
|
},
|
2021-10-10 21:26:22 +00:00
|
|
|
ModuleGraphError::ResolutionError(err) => get_resolution_error_class(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_resolution_error_class(err: &ResolutionError) -> &'static str {
|
|
|
|
match err {
|
2022-01-31 22:33:57 +00:00
|
|
|
ResolutionError::ResolverError { error, .. } => {
|
|
|
|
get_error_class_name(error.as_ref())
|
2021-10-10 21:26:22 +00:00
|
|
|
}
|
|
|
|
_ => "TypeError",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:54:22 +00:00
|
|
|
pub fn get_error_class_name(e: &AnyError) -> &'static str {
|
2020-12-13 18:45:53 +00:00
|
|
|
deno_runtime::errors::get_error_class_name(e)
|
2020-09-14 16:48:57 +00:00
|
|
|
.or_else(|| {
|
|
|
|
e.downcast_ref::<ImportMapError>()
|
|
|
|
.map(get_import_map_error_class)
|
|
|
|
})
|
2021-06-11 13:03:42 +00:00
|
|
|
.or_else(|| e.downcast_ref::<Diagnostic>().map(get_diagnostic_class))
|
2021-10-10 21:26:22 +00: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 16:48:57 +00:00
|
|
|
.unwrap_or_else(|| {
|
2023-02-15 18:20:40 +00: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 14:33:56 +00:00
|
|
|
"Error"
|
2020-09-14 16:48:57 +00:00
|
|
|
})
|
2020-08-25 22:22:15 +00:00
|
|
|
}
|