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;
|
2023-10-20 00:02:08 -04:00
|
|
|
use deno_graph::source::ResolveError;
|
2023-03-21 11:46:40 -04:00
|
|
|
use deno_graph::ModuleError;
|
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;
|
2023-10-05 14:49:09 -04:00
|
|
|
use std::fmt::Write;
|
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-03-21 11:46:40 -04: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 09:09:16 -04:00
|
|
|
| ModuleError::UnsupportedImportAttributeType { .. } => "TypeError",
|
|
|
|
ModuleError::Missing(_, _)
|
|
|
|
| ModuleError::MissingDynamic(_, _)
|
|
|
|
| ModuleError::UnknownPackage { .. }
|
|
|
|
| ModuleError::UnknownPackageReq { .. } => "NotFound",
|
2023-03-21 11:46:40 -04:00
|
|
|
},
|
2023-10-24 17:43:19 -04:00
|
|
|
ModuleGraphError::ResolutionError(err)
|
|
|
|
| ModuleGraphError::TypesResolutionError(err) => {
|
|
|
|
get_resolution_error_class(err)
|
|
|
|
}
|
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, .. } => {
|
2023-10-20 00:02:08 -04:00
|
|
|
use ResolveError::*;
|
|
|
|
match error.as_ref() {
|
|
|
|
Specifier(_) => "TypeError",
|
|
|
|
Other(e) => get_error_class_name(e),
|
|
|
|
}
|
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,
|
2023-10-05 14:49:09 -04:00
|
|
|
e.chain().fold(String::new(), |mut output, e| {
|
|
|
|
let _ = write!(output, "\n {e:?}");
|
|
|
|
output
|
|
|
|
})
|
2023-02-15 13:20:40 -05:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|