2024-01-01 14:58:21 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
use std ::borrow ::Cow ;
2024-07-16 18:32:41 -04:00
use std ::fmt ::Write ;
2022-12-15 21:15:25 -05:00
use std ::path ::PathBuf ;
2024-07-09 12:15:03 -04:00
use thiserror ::Error ;
2024-07-25 19:08:14 -04:00
use url ::Url ;
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
use crate ::NodeModuleKind ;
2023-07-27 12:27:01 -04:00
use crate ::NodeResolutionMode ;
2024-07-09 12:15:03 -04:00
macro_rules ! kinded_err {
( $name :ident , $kind_name :ident ) = > {
#[ derive(Error, Debug) ]
#[ error(transparent) ]
pub struct $name ( pub Box < $kind_name > ) ;
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
impl $name {
pub fn as_kind ( & self ) -> & $kind_name {
& self . 0
}
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
pub fn into_kind ( self ) -> $kind_name {
* self . 0
}
}
impl < E > From < E > for $name
where
$kind_name : From < E > ,
{
fn from ( err : E ) -> Self {
$name ( Box ::new ( $kind_name ::from ( err ) ) )
}
}
} ;
2022-08-20 11:31:33 -04:00
}
2024-07-16 18:32:41 -04:00
#[ derive(Debug, Copy, Clone, PartialEq, Eq, Hash) ]
#[ allow(non_camel_case_types) ]
pub enum NodeJsErrorCode {
ERR_INVALID_MODULE_SPECIFIER ,
ERR_INVALID_PACKAGE_CONFIG ,
ERR_INVALID_PACKAGE_TARGET ,
ERR_MODULE_NOT_FOUND ,
ERR_PACKAGE_IMPORT_NOT_DEFINED ,
ERR_PACKAGE_PATH_NOT_EXPORTED ,
ERR_UNKNOWN_FILE_EXTENSION ,
ERR_UNSUPPORTED_DIR_IMPORT ,
ERR_UNSUPPORTED_ESM_URL_SCHEME ,
2024-07-23 20:22:24 -04:00
/// Deno specific since Node doesn't support TypeScript.
ERR_TYPES_NOT_FOUND ,
2024-07-16 18:32:41 -04:00
}
impl std ::fmt ::Display for NodeJsErrorCode {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
write! ( f , " {} " , self . as_str ( ) )
}
}
impl NodeJsErrorCode {
pub fn as_str ( & self ) -> & 'static str {
use NodeJsErrorCode ::* ;
match self {
ERR_INVALID_MODULE_SPECIFIER = > " ERR_INVALID_MODULE_SPECIFIER " ,
ERR_INVALID_PACKAGE_CONFIG = > " ERR_INVALID_PACKAGE_CONFIG " ,
ERR_INVALID_PACKAGE_TARGET = > " ERR_INVALID_PACKAGE_TARGET " ,
ERR_MODULE_NOT_FOUND = > " ERR_MODULE_NOT_FOUND " ,
ERR_PACKAGE_IMPORT_NOT_DEFINED = > " ERR_PACKAGE_IMPORT_NOT_DEFINED " ,
ERR_PACKAGE_PATH_NOT_EXPORTED = > " ERR_PACKAGE_PATH_NOT_EXPORTED " ,
ERR_UNKNOWN_FILE_EXTENSION = > " ERR_UNKNOWN_FILE_EXTENSION " ,
ERR_UNSUPPORTED_DIR_IMPORT = > " ERR_UNSUPPORTED_DIR_IMPORT " ,
ERR_UNSUPPORTED_ESM_URL_SCHEME = > " ERR_UNSUPPORTED_ESM_URL_SCHEME " ,
2024-07-23 20:22:24 -04:00
ERR_TYPES_NOT_FOUND = > " ERR_TYPES_NOT_FOUND " ,
2024-07-16 18:32:41 -04:00
}
}
}
pub trait NodeJsErrorCoded {
fn code ( & self ) -> NodeJsErrorCode ;
}
2024-07-09 12:15:03 -04:00
// todo(https://github.com/denoland/deno_core/issues/810): make this a TypeError
#[ derive(Debug, Clone, Error) ]
#[ error(
2024-07-16 18:32:41 -04:00
" [{}] Invalid module '{}' {}{} " ,
self . code ( ) ,
2024-07-09 12:15:03 -04:00
request ,
reason ,
maybe_referrer . as_ref ( ) . map ( | referrer | format! ( " imported from ' {} ' " , referrer ) ) . unwrap_or_default ( )
) ]
pub struct InvalidModuleSpecifierError {
pub request : String ,
pub reason : Cow < 'static , str > ,
pub maybe_referrer : Option < String > ,
}
2022-08-20 11:31:33 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for InvalidModuleSpecifierError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_INVALID_MODULE_SPECIFIER
}
}
2024-07-23 20:22:24 -04:00
kinded_err! ( LegacyResolveError , LegacyResolveErrorKind ) ;
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
2024-07-23 20:22:24 -04:00
pub enum LegacyResolveErrorKind {
#[ error(transparent) ]
TypesNotFound ( #[ from ] TypesNotFoundError ) ,
2024-07-09 12:15:03 -04:00
#[ error(transparent) ]
2024-07-23 20:22:24 -04:00
ModuleNotFound ( #[ from ] ModuleNotFoundError ) ,
2022-08-20 11:31:33 -04:00
}
2024-07-23 20:22:24 -04:00
impl NodeJsErrorCoded for LegacyResolveError {
2024-07-16 18:32:41 -04:00
fn code ( & self ) -> NodeJsErrorCode {
2024-07-23 20:22:24 -04:00
match self . as_kind ( ) {
LegacyResolveErrorKind ::TypesNotFound ( e ) = > e . code ( ) ,
LegacyResolveErrorKind ::ModuleNotFound ( e ) = > e . code ( ) ,
2024-07-16 18:32:41 -04:00
}
}
}
2024-07-09 12:15:03 -04:00
kinded_err! ( PackageFolderResolveError , PackageFolderResolveErrorKind ) ;
2024-07-23 20:22:24 -04:00
#[ derive(Debug, Error) ]
#[ error(
" Could not find package '{}' from referrer '{}'{}. " ,
package_name ,
referrer ,
referrer_extra . as_ref ( ) . map ( | r | format! ( " ( {} ) " , r ) ) . unwrap_or_default ( )
) ]
pub struct PackageNotFoundError {
pub package_name : String ,
2024-07-25 19:08:14 -04:00
pub referrer : Url ,
2024-07-23 20:22:24 -04:00
/// Extra information about the referrer.
pub referrer_extra : Option < String > ,
}
impl NodeJsErrorCoded for PackageNotFoundError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_MODULE_NOT_FOUND
}
}
#[ derive(Debug, Error) ]
#[ error(
" Could not find referrer npm package '{}'{}. " ,
referrer ,
referrer_extra . as_ref ( ) . map ( | r | format! ( " ( {} ) " , r ) ) . unwrap_or_default ( )
) ]
pub struct ReferrerNotFoundError {
2024-07-25 19:08:14 -04:00
pub referrer : Url ,
2024-07-23 20:22:24 -04:00
/// Extra information about the referrer.
pub referrer_extra : Option < String > ,
}
impl NodeJsErrorCoded for ReferrerNotFoundError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_MODULE_NOT_FOUND
}
}
#[ derive(Debug, Error) ]
#[ error( " Failed resolving '{package_name}' from referrer '{referrer}'. " ) ]
pub struct PackageFolderResolveIoError {
pub package_name : String ,
2024-07-25 19:08:14 -04:00
pub referrer : Url ,
2024-07-23 20:22:24 -04:00
#[ source ]
pub source : std ::io ::Error ,
}
impl NodeJsErrorCoded for PackageFolderResolveIoError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_MODULE_NOT_FOUND
}
}
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageFolderResolveError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
2024-07-23 20:22:24 -04:00
PackageFolderResolveErrorKind ::PackageNotFound ( e ) = > e . code ( ) ,
PackageFolderResolveErrorKind ::ReferrerNotFound ( e ) = > e . code ( ) ,
PackageFolderResolveErrorKind ::Io ( e ) = > e . code ( ) ,
2024-07-16 18:32:41 -04:00
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum PackageFolderResolveErrorKind {
2024-07-23 20:22:24 -04:00
#[ error(transparent) ]
PackageNotFound ( #[ from ] PackageNotFoundError ) ,
#[ error(transparent) ]
ReferrerNotFound ( #[ from ] ReferrerNotFoundError ) ,
#[ error(transparent) ]
Io ( #[ from ] PackageFolderResolveIoError ) ,
2022-08-20 11:31:33 -04:00
}
2024-07-09 12:15:03 -04:00
kinded_err! ( PackageSubpathResolveError , PackageSubpathResolveErrorKind ) ;
2022-08-20 11:31:33 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageSubpathResolveError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
PackageSubpathResolveErrorKind ::PkgJsonLoad ( e ) = > e . code ( ) ,
PackageSubpathResolveErrorKind ::Exports ( e ) = > e . code ( ) ,
2024-07-23 20:22:24 -04:00
PackageSubpathResolveErrorKind ::LegacyResolve ( e ) = > e . code ( ) ,
2024-07-16 18:32:41 -04:00
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum PackageSubpathResolveErrorKind {
#[ error(transparent) ]
2024-07-16 18:32:41 -04:00
PkgJsonLoad ( #[ from ] PackageJsonLoadError ) ,
2024-07-09 12:15:03 -04:00
#[ error(transparent) ]
Exports ( PackageExportsResolveError ) ,
#[ error(transparent) ]
2024-07-23 20:22:24 -04:00
LegacyResolve ( LegacyResolveError ) ,
2024-07-09 12:15:03 -04:00
}
#[ derive(Debug, Error) ]
#[ error(
" Target '{}' not found from '{}'{}{}. " ,
target ,
pkg_json_path . display ( ) ,
maybe_referrer . as_ref ( ) . map ( | r |
format! (
" from{} referrer {} " ,
match referrer_kind {
NodeModuleKind ::Esm = > " " ,
NodeModuleKind ::Cjs = > " cjs " ,
} ,
r
)
) . unwrap_or_default ( ) ,
match mode {
NodeResolutionMode ::Execution = > " " ,
NodeResolutionMode ::Types = > " for types " ,
2022-08-20 11:31:33 -04:00
}
2024-07-09 12:15:03 -04:00
) ]
pub struct PackageTargetNotFoundError {
pub pkg_json_path : PathBuf ,
pub target : String ,
2024-07-25 19:08:14 -04:00
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
pub referrer_kind : NodeModuleKind ,
pub mode : NodeResolutionMode ,
}
2022-08-20 11:31:33 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageTargetNotFoundError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_MODULE_NOT_FOUND
}
}
2024-07-09 12:15:03 -04:00
kinded_err! ( PackageTargetResolveError , PackageTargetResolveErrorKind ) ;
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageTargetResolveError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
PackageTargetResolveErrorKind ::NotFound ( e ) = > e . code ( ) ,
PackageTargetResolveErrorKind ::InvalidPackageTarget ( e ) = > e . code ( ) ,
PackageTargetResolveErrorKind ::InvalidModuleSpecifier ( e ) = > e . code ( ) ,
PackageTargetResolveErrorKind ::PackageResolve ( e ) = > e . code ( ) ,
2024-07-23 20:22:24 -04:00
PackageTargetResolveErrorKind ::TypesNotFound ( e ) = > e . code ( ) ,
2024-07-16 18:32:41 -04:00
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum PackageTargetResolveErrorKind {
#[ error(transparent) ]
NotFound ( #[ from ] PackageTargetNotFoundError ) ,
#[ error(transparent) ]
InvalidPackageTarget ( #[ from ] InvalidPackageTargetError ) ,
#[ error(transparent) ]
InvalidModuleSpecifier ( #[ from ] InvalidModuleSpecifierError ) ,
#[ error(transparent) ]
PackageResolve ( #[ from ] PackageResolveError ) ,
#[ error(transparent) ]
2024-07-23 20:22:24 -04:00
TypesNotFound ( #[ from ] TypesNotFoundError ) ,
2022-08-20 11:31:33 -04:00
}
2024-07-09 12:15:03 -04:00
kinded_err! ( PackageExportsResolveError , PackageExportsResolveErrorKind ) ;
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageExportsResolveError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
PackageExportsResolveErrorKind ::PackagePathNotExported ( e ) = > e . code ( ) ,
PackageExportsResolveErrorKind ::PackageTargetResolve ( e ) = > e . code ( ) ,
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum PackageExportsResolveErrorKind {
#[ error(transparent) ]
PackagePathNotExported ( #[ from ] PackagePathNotExportedError ) ,
#[ error(transparent) ]
PackageTargetResolve ( #[ from ] PackageTargetResolveError ) ,
}
#[ derive(Debug, Error) ]
2024-07-23 20:22:24 -04:00
#[ error(
" [{}] Could not find types for '{}'{} " ,
self . code ( ) ,
self . 0. code_specifier ,
self . 0. maybe_referrer . as_ref ( ) . map ( | r | format! ( " imported from ' {} ' " , r ) ) . unwrap_or_default ( ) ,
) ]
pub struct TypesNotFoundError ( pub Box < TypesNotFoundErrorData > ) ;
#[ derive(Debug) ]
pub struct TypesNotFoundErrorData {
2024-07-25 19:08:14 -04:00
pub code_specifier : Url ,
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
}
2024-07-23 20:22:24 -04:00
impl NodeJsErrorCoded for TypesNotFoundError {
2024-07-16 18:32:41 -04:00
fn code ( & self ) -> NodeJsErrorCode {
2024-07-23 20:22:24 -04:00
NodeJsErrorCode ::ERR_TYPES_NOT_FOUND
2024-07-16 18:32:41 -04:00
}
}
#[ derive(Debug, Error) ]
#[ error(
" [{}] Invalid package config. {} " ,
self . code ( ) ,
self . 0
) ]
pub struct PackageJsonLoadError (
#[ source ]
#[ from ]
2024-07-23 17:34:46 -04:00
pub deno_package_json ::PackageJsonLoadError ,
2024-07-16 18:32:41 -04:00
) ;
impl NodeJsErrorCoded for PackageJsonLoadError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_INVALID_PACKAGE_CONFIG
}
}
2024-07-09 12:15:03 -04:00
kinded_err! ( ClosestPkgJsonError , ClosestPkgJsonErrorKind ) ;
2022-08-20 11:31:33 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for ClosestPkgJsonError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
ClosestPkgJsonErrorKind ::CanonicalizingDir ( e ) = > e . code ( ) ,
ClosestPkgJsonErrorKind ::Load ( e ) = > e . code ( ) ,
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum ClosestPkgJsonErrorKind {
#[ error(transparent) ]
2024-07-16 18:32:41 -04:00
CanonicalizingDir ( #[ from ] CanonicalizingPkgJsonDirError ) ,
#[ error(transparent) ]
Load ( #[ from ] PackageJsonLoadError ) ,
}
#[ derive(Debug, Error) ]
#[ error( " [{}] Failed canonicalizing package.json directory '{}'. " , self.code(), dir_path.display()) ]
pub struct CanonicalizingPkgJsonDirError {
pub dir_path : PathBuf ,
#[ source ]
pub source : std ::io ::Error ,
}
impl NodeJsErrorCoded for CanonicalizingPkgJsonDirError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_MODULE_NOT_FOUND
}
2024-07-09 12:15:03 -04:00
}
// todo(https://github.com/denoland/deno_core/issues/810): make this a TypeError
#[ derive(Debug, Error) ]
#[ error(
2024-07-16 18:32:41 -04:00
" [{}] Package import specifier \" {} \" is not defined{}{} " ,
self . code ( ) ,
2024-07-09 12:15:03 -04:00
name ,
package_json_path . as_ref ( ) . map ( | p | format! ( " in package {} " , p . display ( ) ) ) . unwrap_or_default ( ) ,
maybe_referrer . as_ref ( ) . map ( | r | format! ( " imported from ' {} ' " , r ) ) . unwrap_or_default ( ) ,
) ]
pub struct PackageImportNotDefinedError {
pub name : String ,
pub package_json_path : Option < PathBuf > ,
2024-07-25 19:08:14 -04:00
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
}
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageImportNotDefinedError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_PACKAGE_IMPORT_NOT_DEFINED
}
}
2024-07-09 12:15:03 -04:00
kinded_err! ( PackageImportsResolveError , PackageImportsResolveErrorKind ) ;
#[ derive(Debug, Error) ]
pub enum PackageImportsResolveErrorKind {
#[ error(transparent) ]
ClosestPkgJson ( ClosestPkgJsonError ) ,
#[ error(transparent) ]
InvalidModuleSpecifier ( #[ from ] InvalidModuleSpecifierError ) ,
#[ error(transparent) ]
NotDefined ( #[ from ] PackageImportNotDefinedError ) ,
#[ error(transparent) ]
Target ( #[ from ] PackageTargetResolveError ) ,
}
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageImportsResolveErrorKind {
fn code ( & self ) -> NodeJsErrorCode {
match self {
Self ::ClosestPkgJson ( e ) = > e . code ( ) ,
Self ::InvalidModuleSpecifier ( e ) = > e . code ( ) ,
Self ::NotDefined ( e ) = > e . code ( ) ,
Self ::Target ( e ) = > e . code ( ) ,
}
}
}
2024-07-09 12:15:03 -04:00
kinded_err! ( PackageResolveError , PackageResolveErrorKind ) ;
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackageResolveError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
PackageResolveErrorKind ::ClosestPkgJson ( e ) = > e . code ( ) ,
PackageResolveErrorKind ::InvalidModuleSpecifier ( e ) = > e . code ( ) ,
2024-07-23 20:22:24 -04:00
PackageResolveErrorKind ::PackageFolderResolve ( e ) = > e . code ( ) ,
2024-07-16 18:32:41 -04:00
PackageResolveErrorKind ::ExportsResolve ( e ) = > e . code ( ) ,
PackageResolveErrorKind ::SubpathResolve ( e ) = > e . code ( ) ,
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum PackageResolveErrorKind {
#[ error(transparent) ]
ClosestPkgJson ( #[ from ] ClosestPkgJsonError ) ,
#[ error(transparent) ]
InvalidModuleSpecifier ( #[ from ] InvalidModuleSpecifierError ) ,
#[ error(transparent) ]
2024-07-23 20:22:24 -04:00
PackageFolderResolve ( #[ from ] PackageFolderResolveError ) ,
#[ error(transparent) ]
2024-07-09 12:15:03 -04:00
ExportsResolve ( #[ from ] PackageExportsResolveError ) ,
#[ error(transparent) ]
SubpathResolve ( #[ from ] PackageSubpathResolveError ) ,
}
#[ derive(Debug, Error) ]
2024-07-23 20:22:24 -04:00
#[ error( " Failed joining '{path}' from '{base}'. " ) ]
pub struct NodeResolveRelativeJoinError {
pub path : String ,
2024-07-25 19:08:14 -04:00
pub base : Url ,
2024-07-23 20:22:24 -04:00
#[ source ]
pub source : url ::ParseError ,
}
#[ derive(Debug, Error) ]
#[ error( " Failed resolving specifier from data url referrer. " ) ]
pub struct DataUrlReferrerError {
#[ source ]
pub source : url ::ParseError ,
}
kinded_err! ( NodeResolveError , NodeResolveErrorKind ) ;
#[ derive(Debug, Error) ]
pub enum NodeResolveErrorKind {
#[ error(transparent) ]
RelativeJoin ( #[ from ] NodeResolveRelativeJoinError ) ,
2024-07-09 12:15:03 -04:00
#[ error(transparent) ]
PackageImportsResolve ( #[ from ] PackageImportsResolveError ) ,
#[ error(transparent) ]
UnsupportedEsmUrlScheme ( #[ from ] UnsupportedEsmUrlSchemeError ) ,
2024-07-23 20:22:24 -04:00
#[ error(transparent) ]
DataUrlReferrer ( #[ from ] DataUrlReferrerError ) ,
2024-07-09 12:15:03 -04:00
#[ error(transparent) ]
PackageResolve ( #[ from ] PackageResolveError ) ,
#[ error(transparent) ]
2024-07-23 20:22:24 -04:00
TypesNotFound ( #[ from ] TypesNotFoundError ) ,
2024-07-09 12:15:03 -04:00
#[ error(transparent) ]
FinalizeResolution ( #[ from ] FinalizeResolutionError ) ,
}
kinded_err! ( FinalizeResolutionError , FinalizeResolutionErrorKind ) ;
#[ derive(Debug, Error) ]
pub enum FinalizeResolutionErrorKind {
#[ error(transparent) ]
InvalidModuleSpecifierError ( #[ from ] InvalidModuleSpecifierError ) ,
#[ error(transparent) ]
ModuleNotFound ( #[ from ] ModuleNotFoundError ) ,
#[ error(transparent) ]
UnsupportedDirImport ( #[ from ] UnsupportedDirImportError ) ,
}
2024-07-23 20:22:24 -04:00
impl NodeJsErrorCoded for FinalizeResolutionError {
fn code ( & self ) -> NodeJsErrorCode {
match self . as_kind ( ) {
FinalizeResolutionErrorKind ::InvalidModuleSpecifierError ( e ) = > e . code ( ) ,
FinalizeResolutionErrorKind ::ModuleNotFound ( e ) = > e . code ( ) ,
FinalizeResolutionErrorKind ::UnsupportedDirImport ( e ) = > e . code ( ) ,
}
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
#[ error(
2024-07-16 18:32:41 -04:00
" [{}] Cannot find {} '{}'{} " ,
self . code ( ) ,
2024-07-09 12:15:03 -04:00
typ ,
specifier ,
maybe_referrer . as_ref ( ) . map ( | referrer | format! ( " imported from ' {} ' " , referrer ) ) . unwrap_or_default ( )
) ]
pub struct ModuleNotFoundError {
2024-07-25 19:08:14 -04:00
pub specifier : Url ,
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
pub typ : & 'static str ,
}
2024-07-23 20:22:24 -04:00
impl NodeJsErrorCoded for ModuleNotFoundError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_MODULE_NOT_FOUND
2024-07-16 18:32:41 -04:00
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
#[ error(
2024-07-16 18:32:41 -04:00
" [{}] Directory import '{}' is not supported resolving ES modules{} " ,
self . code ( ) ,
2024-07-09 12:15:03 -04:00
dir_url ,
maybe_referrer . as_ref ( ) . map ( | referrer | format! ( " imported from ' {} ' " , referrer ) ) . unwrap_or_default ( ) ,
) ]
pub struct UnsupportedDirImportError {
2024-07-25 19:08:14 -04:00
pub dir_url : Url ,
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
}
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for UnsupportedDirImportError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_UNSUPPORTED_DIR_IMPORT
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug) ]
pub struct InvalidPackageTargetError {
pub pkg_json_path : PathBuf ,
pub sub_path : String ,
pub target : String ,
pub is_import : bool ,
2024-07-25 19:08:14 -04:00
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
}
impl std ::error ::Error for InvalidPackageTargetError { }
impl std ::fmt ::Display for InvalidPackageTargetError {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
let rel_error = ! self . is_import
& & ! self . target . is_empty ( )
& & ! self . target . starts_with ( " ./ " ) ;
2024-07-16 18:32:41 -04:00
f . write_char ( '[' ) ? ;
f . write_str ( self . code ( ) . as_str ( ) ) ? ;
f . write_char ( ']' ) ? ;
2024-07-09 12:15:03 -04:00
if self . sub_path = = " . " {
assert! ( ! self . is_import ) ;
write! (
f ,
" Invalid \" exports \" main target {} defined in the package config {} " ,
self . target ,
self . pkg_json_path . display ( )
) ? ;
} else {
let ie = if self . is_import { " imports " } else { " exports " } ;
write! (
f ,
" Invalid \" {} \" target {} defined for '{}' in the package config {} " ,
ie ,
self . target ,
self . sub_path ,
self . pkg_json_path . display ( )
) ? ;
} ;
if let Some ( referrer ) = & self . maybe_referrer {
write! ( f , " imported from '{}' " , referrer ) ? ;
2022-10-03 13:10:53 -04:00
}
2024-07-09 12:15:03 -04:00
if rel_error {
write! ( f , " ; target must start with \" ./ \" " ) ? ;
2022-10-03 13:10:53 -04:00
}
2024-07-09 12:15:03 -04:00
Ok ( ( ) )
2022-10-03 13:10:53 -04:00
}
2024-07-09 12:15:03 -04:00
}
2022-10-03 13:10:53 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for InvalidPackageTargetError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_INVALID_PACKAGE_TARGET
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug) ]
pub struct PackagePathNotExportedError {
pub pkg_json_path : PathBuf ,
pub subpath : String ,
2024-07-25 19:08:14 -04:00
pub maybe_referrer : Option < Url > ,
2024-07-09 12:15:03 -04:00
pub mode : NodeResolutionMode ,
}
2022-08-20 11:31:33 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for PackagePathNotExportedError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_PACKAGE_PATH_NOT_EXPORTED
}
}
2024-07-09 12:15:03 -04:00
impl std ::error ::Error for PackagePathNotExportedError { }
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
impl std ::fmt ::Display for PackagePathNotExportedError {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
2024-07-16 18:32:41 -04:00
f . write_char ( '[' ) ? ;
f . write_str ( self . code ( ) . as_str ( ) ) ? ;
f . write_char ( ']' ) ? ;
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
let types_msg = match self . mode {
NodeResolutionMode ::Execution = > String ::new ( ) ,
NodeResolutionMode ::Types = > " for types " . to_string ( ) ,
} ;
if self . subpath = = " . " {
write! (
f ,
" No \" exports \" main defined{} in '{}' " ,
types_msg ,
self . pkg_json_path . display ( )
) ? ;
} else {
write! (
f ,
" Package subpath '{}' is not defined{} by \" exports \" in '{}' " ,
self . subpath ,
types_msg ,
self . pkg_json_path . display ( )
) ? ;
} ;
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
if let Some ( referrer ) = & self . maybe_referrer {
write! ( f , " imported from '{}' " , referrer ) ? ;
}
Ok ( ( ) )
2022-08-20 11:31:33 -04:00
}
2024-07-09 12:15:03 -04:00
}
2022-08-20 11:31:33 -04:00
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Clone, Error) ]
#[ error(
2024-07-16 18:32:41 -04:00
" [{}] Only file and data URLs are supported by the default ESM loader.{} Received protocol '{}' " ,
self . code ( ) ,
2024-07-09 12:15:03 -04:00
if cfg! ( windows ) & & url_scheme . len ( ) = = 2 { " On Windows, absolute path must be valid file:// URLS. " } else { " " } ,
url_scheme
) ]
pub struct UnsupportedEsmUrlSchemeError {
pub url_scheme : String ,
}
2022-08-20 11:31:33 -04:00
2024-07-16 18:32:41 -04:00
impl NodeJsErrorCoded for UnsupportedEsmUrlSchemeError {
fn code ( & self ) -> NodeJsErrorCode {
NodeJsErrorCode ::ERR_UNSUPPORTED_ESM_URL_SCHEME
}
}
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum ResolvePkgJsonBinExportError {
#[ error(transparent) ]
2024-07-16 18:32:41 -04:00
PkgJsonLoad ( #[ from ] PackageJsonLoadError ) ,
2024-07-09 12:15:03 -04:00
#[ error( " Failed resolving binary export. '{}' did not exist " , pkg_json_path.display()) ]
MissingPkgJson { pkg_json_path : PathBuf } ,
#[ error( " Failed resolving binary export. {message} " ) ]
InvalidBinProperty { message : String } ,
2022-08-20 11:31:33 -04:00
}
2022-09-05 06:36:35 -04:00
2024-07-09 12:15:03 -04:00
#[ derive(Debug, Error) ]
pub enum ResolveBinaryCommandsError {
#[ error(transparent) ]
2024-07-16 18:32:41 -04:00
PkgJsonLoad ( #[ from ] PackageJsonLoadError ) ,
2024-07-09 12:15:03 -04:00
#[ error( " '{}' did not have a name " , pkg_json_path.display()) ]
MissingPkgJsonName { pkg_json_path : PathBuf } ,
2022-09-05 06:36:35 -04:00
}
2023-07-27 12:27:01 -04:00
#[ cfg(test) ]
mod test {
use super ::* ;
#[ test ]
fn types_resolution_package_path_not_exported ( ) {
let separator_char = if cfg! ( windows ) { '\\' } else { '/' } ;
assert_eq! (
2024-07-09 12:15:03 -04:00
PackagePathNotExportedError {
pkg_json_path : PathBuf ::from ( " test_path " ) . join ( " package.json " ) ,
subpath : " ./jsx-runtime " . to_string ( ) ,
maybe_referrer : None ,
mode : NodeResolutionMode ::Types
} . to_string ( ) ,
2023-07-27 12:27:01 -04:00
format! ( " [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './jsx-runtime' is not defined for types by \" exports \" in 'test_path {separator_char} package.json' " )
) ;
assert_eq! (
2024-07-09 12:15:03 -04:00
PackagePathNotExportedError {
pkg_json_path : PathBuf ::from ( " test_path " ) . join ( " package.json " ) ,
subpath : " . " . to_string ( ) ,
maybe_referrer : None ,
mode : NodeResolutionMode ::Types
} . to_string ( ) ,
2023-07-27 12:27:01 -04:00
format! ( " [ERR_PACKAGE_PATH_NOT_EXPORTED] No \" exports \" main defined for types in 'test_path {separator_char} package.json' " )
) ;
}
}