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;
|
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-02-10 03:00:23 +00:00
|
|
|
ModuleGraphError::LoadingErr(_, _, err) => {
|
|
|
|
get_error_class_name(err.as_ref())
|
|
|
|
}
|
2022-12-06 19:12:51 +00:00
|
|
|
ModuleGraphError::InvalidTypeAssertion { .. } => "SyntaxError",
|
2021-10-10 21:26:22 +00:00
|
|
|
ModuleGraphError::ParseErr(_, diagnostic) => {
|
|
|
|
get_diagnostic_class(diagnostic)
|
|
|
|
}
|
|
|
|
ModuleGraphError::ResolutionError(err) => get_resolution_error_class(err),
|
2023-02-10 03:00:23 +00:00
|
|
|
ModuleGraphError::UnsupportedMediaType { .. }
|
|
|
|
| ModuleGraphError::UnsupportedImportAssertionType { .. } => "TypeError",
|
2023-02-15 16:30:54 +00:00
|
|
|
ModuleGraphError::Missing(_, _)
|
|
|
|
| ModuleGraphError::MissingDynamic(_, _) => "NotFound",
|
2021-10-10 21:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(|| {
|
2021-11-07 14:33:56 +00:00
|
|
|
eprintln!(
|
2021-01-09 00:57:37 +00:00
|
|
|
"Error '{}' contains boxed error of unknown type:{}",
|
|
|
|
e,
|
2023-01-27 15:43:16 +00:00
|
|
|
e.chain().map(|e| format!("\n {e:?}")).collect::<String>()
|
2021-01-09 00:57:37 +00:00
|
|
|
);
|
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
|
|
|
}
|