2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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.
|
|
|
|
|
2024-02-08 20:40:26 -05:00
|
|
|
use deno_ast::ParseDiagnostic;
|
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;
|
2024-05-28 14:58:43 -04:00
|
|
|
use deno_graph::ModuleLoadError;
|
2021-10-10 17:26:22 -04:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:40:26 -05:00
|
|
|
fn get_diagnostic_class(_: &ParseDiagnostic) -> &'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 {
|
2024-05-28 14:58:43 -04:00
|
|
|
use deno_graph::JsrLoadError;
|
|
|
|
use deno_graph::NpmLoadError;
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
match err {
|
2024-05-28 14:58:43 -04:00
|
|
|
ModuleGraphError::ResolutionError(err)
|
|
|
|
| ModuleGraphError::TypesResolutionError(err) => {
|
|
|
|
get_resolution_error_class(err)
|
|
|
|
}
|
2023-03-21 11:46:40 -04:00
|
|
|
ModuleGraphError::ModuleError(err) => match err {
|
|
|
|
ModuleError::InvalidTypeAssertion { .. } => "SyntaxError",
|
|
|
|
ModuleError::ParseErr(_, diagnostic) => get_diagnostic_class(diagnostic),
|
|
|
|
ModuleError::UnsupportedMediaType { .. }
|
2023-09-07 09:09:16 -04:00
|
|
|
| ModuleError::UnsupportedImportAttributeType { .. } => "TypeError",
|
2024-05-28 14:58:43 -04:00
|
|
|
ModuleError::Missing(_, _) | ModuleError::MissingDynamic(_, _) => {
|
|
|
|
"NotFound"
|
|
|
|
}
|
|
|
|
ModuleError::LoadingErr(_, _, err) => match err {
|
|
|
|
ModuleLoadError::Loader(err) => get_error_class_name(err.as_ref()),
|
|
|
|
ModuleLoadError::HttpsChecksumIntegrity(_)
|
|
|
|
| ModuleLoadError::TooManyRedirects => "Error",
|
|
|
|
ModuleLoadError::NodeUnknownBuiltinModule(_) => "NotFound",
|
|
|
|
ModuleLoadError::Decode(_) => "TypeError",
|
|
|
|
ModuleLoadError::Npm(err) => match err {
|
|
|
|
NpmLoadError::NotSupportedEnvironment
|
|
|
|
| NpmLoadError::PackageReqResolution(_)
|
|
|
|
| NpmLoadError::RegistryInfo(_) => "Error",
|
|
|
|
NpmLoadError::PackageReqReferenceParse(_) => "TypeError",
|
|
|
|
},
|
|
|
|
ModuleLoadError::Jsr(err) => match err {
|
|
|
|
JsrLoadError::UnsupportedManifestChecksum
|
|
|
|
| JsrLoadError::PackageFormat(_) => "TypeError",
|
|
|
|
JsrLoadError::ContentLoadExternalSpecifier
|
|
|
|
| JsrLoadError::ContentLoad(_)
|
|
|
|
| JsrLoadError::ContentChecksumIntegrity(_)
|
|
|
|
| JsrLoadError::PackageManifestLoad(_, _)
|
|
|
|
| JsrLoadError::PackageVersionManifestChecksumIntegrity(..)
|
|
|
|
| JsrLoadError::PackageVersionManifestLoad(_, _)
|
|
|
|
| JsrLoadError::RedirectInPackage(_) => "Error",
|
|
|
|
JsrLoadError::PackageNotFound(_)
|
|
|
|
| JsrLoadError::PackageReqNotFound(_)
|
|
|
|
| JsrLoadError::PackageVersionNotFound(_)
|
|
|
|
| JsrLoadError::UnknownExport { .. } => "NotFound",
|
|
|
|
},
|
|
|
|
},
|
2024-11-19 11:56:04 -05:00
|
|
|
ModuleError::WasmParseErr(_, _) => "SyntaxError",
|
2023-03-21 11:46:40 -04:00
|
|
|
},
|
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",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 19:57:57 -05:00
|
|
|
fn get_try_from_int_error_class(_: &std::num::TryFromIntError) -> &'static str {
|
|
|
|
"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)
|
|
|
|
})
|
2024-02-08 20:40:26 -05:00
|
|
|
.or_else(|| {
|
|
|
|
e.downcast_ref::<ParseDiagnostic>()
|
|
|
|
.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)
|
|
|
|
})
|
2024-11-06 19:57:57 -05:00
|
|
|
.or_else(|| {
|
|
|
|
e.downcast_ref::<std::num::TryFromIntError>()
|
|
|
|
.map(get_try_from_int_error_class)
|
|
|
|
})
|
2024-07-29 12:58:04 -04:00
|
|
|
.unwrap_or("Error")
|
2020-08-25 18:22:15 -04:00
|
|
|
}
|