2023-01-02 16:00:42 -05:00
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2022-08-20 11:31:33 -04:00
2022-12-15 21:15:25 -05:00
use std ::path ::PathBuf ;
2022-08-20 11:31:33 -04:00
use deno_core ::error ::generic_error ;
use deno_core ::error ::type_error ;
use deno_core ::error ::AnyError ;
2022-09-05 06:36:35 -04:00
use deno_core ::url ::Url ;
2022-08-20 11:31:33 -04:00
2023-07-27 12:27:01 -04:00
use crate ::NodeResolutionMode ;
2022-08-20 11:31:33 -04:00
pub fn err_invalid_module_specifier (
request : & str ,
reason : & str ,
maybe_base : Option < String > ,
) -> AnyError {
let mut msg = format! (
2023-01-27 10:43:16 -05:00
" [ERR_INVALID_MODULE_SPECIFIER] Invalid module \" {request} \" {reason} "
2022-08-20 11:31:33 -04:00
) ;
if let Some ( base ) = maybe_base {
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} imported from {base} " ) ;
2022-08-20 11:31:33 -04:00
}
type_error ( msg )
}
#[ allow(unused) ]
pub fn err_invalid_package_config (
path : & str ,
maybe_base : Option < String > ,
maybe_message : Option < String > ,
) -> AnyError {
2023-01-27 10:43:16 -05:00
let mut msg =
format! ( " [ERR_INVALID_PACKAGE_CONFIG] Invalid package config {path} " ) ;
2022-08-20 11:31:33 -04:00
if let Some ( base ) = maybe_base {
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} while importing {base} " ) ;
2022-08-20 11:31:33 -04:00
}
if let Some ( message ) = maybe_message {
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} . {message} " ) ;
2022-08-20 11:31:33 -04:00
}
generic_error ( msg )
}
#[ allow(unused) ]
pub fn err_module_not_found ( path : & str , base : & str , typ : & str ) -> AnyError {
generic_error ( format! (
2023-01-27 10:43:16 -05:00
" [ERR_MODULE_NOT_FOUND] Cannot find {typ} \" {path} \" imported from \" {base} \" "
2022-08-20 11:31:33 -04:00
) )
}
pub fn err_invalid_package_target (
2023-10-04 23:05:12 -04:00
pkg_path : & str ,
key : & str ,
2022-08-20 11:31:33 -04:00
target : String ,
is_import : bool ,
2022-08-30 14:09:22 -04:00
maybe_referrer : Option < String > ,
2022-08-20 11:31:33 -04:00
) -> AnyError {
let rel_error = ! is_import & & ! target . is_empty ( ) & & ! target . starts_with ( " ./ " ) ;
let mut msg = " [ERR_INVALID_PACKAGE_TARGET] " . to_string ( ) ;
2022-12-15 21:15:25 -05:00
let pkg_json_path = PathBuf ::from ( pkg_path ) . join ( " package.json " ) ;
2022-08-20 11:31:33 -04:00
if key = = " . " {
assert! ( ! is_import ) ;
2022-12-15 21:15:25 -05:00
msg = format! (
" {} Invalid \" exports \" main target {} defined in the package config {} " ,
msg ,
target ,
pkg_json_path . display ( )
)
2022-08-20 11:31:33 -04:00
} else {
let ie = if is_import { " imports " } else { " exports " } ;
2022-12-15 21:15:25 -05:00
msg = format! (
" {} Invalid \" {} \" target {} defined for '{}' in the package config {} " ,
msg ,
ie ,
target ,
key ,
pkg_json_path . display ( )
)
2022-08-20 11:31:33 -04:00
} ;
2022-08-30 14:09:22 -04:00
if let Some ( base ) = maybe_referrer {
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} imported from {base} " ) ;
2022-08-20 11:31:33 -04:00
} ;
if rel_error {
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} ; target must start with \" ./ \" " ) ;
2022-08-20 11:31:33 -04:00
}
generic_error ( msg )
}
pub fn err_package_path_not_exported (
2022-10-03 13:10:53 -04:00
mut pkg_path : String ,
2023-10-04 23:05:12 -04:00
subpath : & str ,
2022-08-30 14:09:22 -04:00
maybe_referrer : Option < String > ,
2023-07-27 12:27:01 -04:00
mode : NodeResolutionMode ,
2022-08-20 11:31:33 -04:00
) -> AnyError {
let mut msg = " [ERR_PACKAGE_PATH_NOT_EXPORTED] " . to_string ( ) ;
2022-10-03 13:10:53 -04:00
#[ cfg(windows) ]
{
if ! pkg_path . ends_with ( '\\' ) {
pkg_path . push ( '\\' ) ;
}
}
#[ cfg(not(windows)) ]
{
if ! pkg_path . ends_with ( '/' ) {
pkg_path . push ( '/' ) ;
}
}
2023-07-27 12:27:01 -04:00
let types_msg = match mode {
NodeResolutionMode ::Execution = > String ::new ( ) ,
NodeResolutionMode ::Types = > " for types " . to_string ( ) ,
} ;
2022-08-20 11:31:33 -04:00
if subpath = = " . " {
2023-01-27 10:43:16 -05:00
msg =
2023-07-27 12:27:01 -04:00
format! ( " {msg} No \" exports \" main defined {types_msg} in ' {pkg_path} package.json' " ) ;
2022-08-20 11:31:33 -04:00
} else {
2023-07-27 12:27:01 -04:00
msg = format! ( " {msg} Package subpath ' {subpath} ' is not defined {types_msg} by \" exports \" in ' {pkg_path} package.json' " ) ;
2022-08-20 11:31:33 -04:00
} ;
2022-08-30 14:09:22 -04:00
if let Some ( referrer ) = maybe_referrer {
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} imported from ' {referrer} ' " ) ;
2022-08-20 11:31:33 -04:00
}
generic_error ( msg )
}
pub fn err_package_import_not_defined (
specifier : & str ,
package_path : Option < String > ,
base : & str ,
) -> AnyError {
let mut msg = format! (
2023-11-04 12:41:51 -04:00
" [ERR_PACKAGE_IMPORT_NOT_DEFINED] Package import specifier \" {specifier} \" is not defined "
2022-08-20 11:31:33 -04:00
) ;
if let Some ( package_path ) = package_path {
2023-11-04 12:41:51 -04:00
let pkg_json_path = PathBuf ::from ( package_path ) . join ( " package.json " ) ;
msg = format! ( " {} in package {} " , msg , pkg_json_path . display ( ) ) ;
2022-08-20 11:31:33 -04:00
}
2023-01-27 10:43:16 -05:00
msg = format! ( " {msg} imported from {base} " ) ;
2022-08-20 11:31:33 -04:00
type_error ( msg )
}
2022-09-05 06:36:35 -04:00
pub fn err_unsupported_dir_import ( path : & str , base : & str ) -> AnyError {
2023-01-27 10:43:16 -05:00
generic_error ( format! ( " [ERR_UNSUPPORTED_DIR_IMPORT] Directory import ' {path} ' is not supported resolving ES modules imported from {base} " ) )
2022-09-05 06:36:35 -04:00
}
pub fn err_unsupported_esm_url_scheme ( url : & Url ) -> AnyError {
let mut msg =
" [ERR_UNSUPPORTED_ESM_URL_SCHEME] Only file and data URLS are supported by the default ESM loader "
. to_string ( ) ;
if cfg! ( window ) & & url . scheme ( ) . len ( ) = = 2 {
2023-01-27 10:43:16 -05:00
msg =
format! ( " {msg} . On Windows, absolute path must be valid file:// URLs " ) ;
2022-09-05 06:36:35 -04:00
}
msg = format! ( " {} . Received protocol ' {} ' " , msg , url . scheme ( ) ) ;
generic_error ( msg )
}
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! (
err_package_path_not_exported (
" test_path " . to_string ( ) ,
2023-10-04 23:05:12 -04:00
" ./jsx-runtime " ,
2023-07-27 12:27:01 -04:00
None ,
NodeResolutionMode ::Types ,
)
. to_string ( ) ,
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! (
err_package_path_not_exported (
" test_path " . to_string ( ) ,
2023-10-04 23:05:12 -04:00
" . " ,
2023-07-27 12:27:01 -04:00
None ,
NodeResolutionMode ::Types ,
)
. to_string ( ) ,
format! ( " [ERR_PACKAGE_PATH_NOT_EXPORTED] No \" exports \" main defined for types in 'test_path {separator_char} package.json' " )
) ;
}
}