|
| 1 | +// Manually “tree shaken” from: |
| 2 | +// <https://github.com/nodejs/node/blob/89f592c/lib/internal/errors.js> |
| 3 | +import assert from 'assert' |
| 4 | +// Needed for types. |
| 5 | +// eslint-disable-next-line no-unused-vars |
| 6 | +import { format, inspect } from 'util' |
| 7 | + |
| 8 | +const isWindows = process.platform === 'win32' |
| 9 | + |
| 10 | +const own = {}.hasOwnProperty |
| 11 | + |
| 12 | +export const codes = {} |
| 13 | + |
| 14 | +/** |
| 15 | + * @typedef {(...args: unknown[]) => string} MessageFunction |
| 16 | + */ |
| 17 | + |
| 18 | +/** @type {Map<string, MessageFunction|string>} */ |
| 19 | +const messages = new Map() |
| 20 | +const nodeInternalPrefix = '__node_internal_' |
| 21 | +/** @type {number} */ |
| 22 | +let userStackTraceLimit |
| 23 | + |
| 24 | +codes.ERR_INVALID_MODULE_SPECIFIER = createError( |
| 25 | + 'ERR_INVALID_MODULE_SPECIFIER', |
| 26 | + /** |
| 27 | + * @param {string} request |
| 28 | + * @param {string} reason |
| 29 | + * @param {string} [base] |
| 30 | + */ |
| 31 | + (request, reason, base = undefined) => { |
| 32 | + return `Invalid module "${request}" ${reason}${ |
| 33 | + base ? ` imported from ${base}` : '' |
| 34 | + }` |
| 35 | + }, |
| 36 | + TypeError |
| 37 | +) |
| 38 | + |
| 39 | +codes.ERR_INVALID_PACKAGE_CONFIG = createError( |
| 40 | + 'ERR_INVALID_PACKAGE_CONFIG', |
| 41 | + /** |
| 42 | + * @param {string} path |
| 43 | + * @param {string} [base] |
| 44 | + * @param {string} [message] |
| 45 | + */ |
| 46 | + (path, base, message) => { |
| 47 | + return `Invalid package config ${path}${ |
| 48 | + base ? ` while importing ${base}` : '' |
| 49 | + }${message ? `. ${message}` : ''}` |
| 50 | + }, |
| 51 | + Error |
| 52 | +) |
| 53 | + |
| 54 | +codes.ERR_INVALID_PACKAGE_TARGET = createError( |
| 55 | + 'ERR_INVALID_PACKAGE_TARGET', |
| 56 | + /** |
| 57 | + * @param {string} pkgPath |
| 58 | + * @param {string} key |
| 59 | + * @param {unknown} target |
| 60 | + * @param {boolean} [isImport=false] |
| 61 | + * @param {string} [base] |
| 62 | + */ |
| 63 | + (pkgPath, key, target, isImport = false, base = undefined) => { |
| 64 | + const relError = |
| 65 | + typeof target === 'string' && |
| 66 | + !isImport && |
| 67 | + target.length > 0 && |
| 68 | + !target.startsWith('./') |
| 69 | + if (key === '.') { |
| 70 | + assert(isImport === false) |
| 71 | + return ( |
| 72 | + `Invalid "exports" main target ${JSON.stringify(target)} defined ` + |
| 73 | + `in the package config ${pkgPath}package.json${ |
| 74 | + base ? ` imported from ${base}` : '' |
| 75 | + }${relError ? '; targets must start with "./"' : ''}` |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + return `Invalid "${ |
| 80 | + isImport ? 'imports' : 'exports' |
| 81 | + }" target ${JSON.stringify( |
| 82 | + target |
| 83 | + )} defined for '${key}' in the package config ${pkgPath}package.json${ |
| 84 | + base ? ` imported from ${base}` : '' |
| 85 | + }${relError ? '; targets must start with "./"' : ''}` |
| 86 | + }, |
| 87 | + Error |
| 88 | +) |
| 89 | + |
| 90 | +codes.ERR_MODULE_NOT_FOUND = createError( |
| 91 | + 'ERR_MODULE_NOT_FOUND', |
| 92 | + /** |
| 93 | + * @param {string} path |
| 94 | + * @param {string} base |
| 95 | + * @param {string} [type] |
| 96 | + */ |
| 97 | + (path, base, type = 'package') => { |
| 98 | + return `Cannot find ${type} '${path}' imported from ${base}` |
| 99 | + }, |
| 100 | + Error |
| 101 | +) |
| 102 | + |
| 103 | +codes.ERR_PACKAGE_IMPORT_NOT_DEFINED = createError( |
| 104 | + 'ERR_PACKAGE_IMPORT_NOT_DEFINED', |
| 105 | + /** |
| 106 | + * @param {string} specifier |
| 107 | + * @param {string} packagePath |
| 108 | + * @param {string} base |
| 109 | + */ |
| 110 | + (specifier, packagePath, base) => { |
| 111 | + return `Package import specifier "${specifier}" is not defined${ |
| 112 | + packagePath ? ` in package ${packagePath}package.json` : '' |
| 113 | + } imported from ${base}` |
| 114 | + }, |
| 115 | + TypeError |
| 116 | +) |
| 117 | + |
| 118 | +codes.ERR_PACKAGE_PATH_NOT_EXPORTED = createError( |
| 119 | + 'ERR_PACKAGE_PATH_NOT_EXPORTED', |
| 120 | + /** |
| 121 | + * @param {string} pkgPath |
| 122 | + * @param {string} subpath |
| 123 | + * @param {string} [base] |
| 124 | + */ |
| 125 | + (pkgPath, subpath, base = undefined) => { |
| 126 | + if (subpath === '.') { |
| 127 | + return `No "exports" main defined in ${pkgPath}package.json${ |
| 128 | + base ? ` imported from ${base}` : '' |
| 129 | + }` |
| 130 | + } |
| 131 | + return `Package subpath '${subpath}' is not defined by "exports" in ${pkgPath}package.json${ |
| 132 | + base ? ` imported from ${base}` : '' |
| 133 | + }` |
| 134 | + }, |
| 135 | + Error |
| 136 | +) |
| 137 | + |
| 138 | +codes.ERR_UNSUPPORTED_DIR_IMPORT = createError( |
| 139 | + 'ERR_UNSUPPORTED_DIR_IMPORT', |
| 140 | + "Directory import '%s' is not supported " + |
| 141 | + 'resolving ES modules imported from %s', |
| 142 | + Error |
| 143 | +) |
| 144 | + |
| 145 | +codes.ERR_UNKNOWN_FILE_EXTENSION = createError( |
| 146 | + 'ERR_UNKNOWN_FILE_EXTENSION', |
| 147 | + 'Unknown file extension "%s" for %s', |
| 148 | + TypeError |
| 149 | +) |
| 150 | + |
| 151 | +codes.ERR_INVALID_ARG_VALUE = createError( |
| 152 | + 'ERR_INVALID_ARG_VALUE', |
| 153 | + /** |
| 154 | + * @param {string} name |
| 155 | + * @param {unknown} value |
| 156 | + * @param {string} [reason='is invalid'] |
| 157 | + */ |
| 158 | + (name, value, reason = 'is invalid') => { |
| 159 | + let inspected = inspect(value) |
| 160 | + |
| 161 | + if (inspected.length > 128) { |
| 162 | + inspected = `${inspected.slice(0, 128)}...` |
| 163 | + } |
| 164 | + |
| 165 | + const type = name.includes('.') ? 'property' : 'argument' |
| 166 | + |
| 167 | + return `The ${type} '${name}' ${reason}. Received ${inspected}` |
| 168 | + }, |
| 169 | + TypeError |
| 170 | + // Note: extra classes have been shaken out. |
| 171 | + // , RangeError |
| 172 | +) |
| 173 | + |
| 174 | +codes.ERR_UNSUPPORTED_ESM_URL_SCHEME = createError( |
| 175 | + 'ERR_UNSUPPORTED_ESM_URL_SCHEME', |
| 176 | + /** |
| 177 | + * @param {URL} url |
| 178 | + */ |
| 179 | + (url) => { |
| 180 | + let message = |
| 181 | + 'Only file and data URLs are supported by the default ESM loader' |
| 182 | + |
| 183 | + if (isWindows && url.protocol.length === 2) { |
| 184 | + message += '. On Windows, absolute paths must be valid file:// URLs' |
| 185 | + } |
| 186 | + |
| 187 | + message += `. Received protocol '${url.protocol}'` |
| 188 | + return message |
| 189 | + }, |
| 190 | + Error |
| 191 | +) |
| 192 | + |
| 193 | +/** |
| 194 | + * Utility function for registering the error codes. Only used here. Exported |
| 195 | + * *only* to allow for testing. |
| 196 | + * @param {string} sym |
| 197 | + * @param {MessageFunction|string} value |
| 198 | + * @param {ErrorConstructor} def |
| 199 | + * @returns {new (...args: unknown[]) => Error} |
| 200 | + */ |
| 201 | +function createError (sym, value, def) { |
| 202 | + // Special case for SystemError that formats the error message differently |
| 203 | + // The SystemErrors only have SystemError as their base classes. |
| 204 | + messages.set(sym, value) |
| 205 | + |
| 206 | + return makeNodeErrorWithCode(def, sym) |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * @param {ErrorConstructor} Base |
| 211 | + * @param {string} key |
| 212 | + * @returns {ErrorConstructor} |
| 213 | + */ |
| 214 | +function makeNodeErrorWithCode (Base, key) { |
| 215 | + // @ts-expect-error It’s a Node error. |
| 216 | + return NodeError |
| 217 | + /** |
| 218 | + * @param {unknown[]} args |
| 219 | + */ |
| 220 | + function NodeError (...args) { |
| 221 | + const limit = Error.stackTraceLimit |
| 222 | + if (isErrorStackTraceLimitWritable()) { Error.stackTraceLimit = 0 } |
| 223 | + const error = new Base() |
| 224 | + // Reset the limit and setting the name property. |
| 225 | + if (isErrorStackTraceLimitWritable()) { Error.stackTraceLimit = limit } |
| 226 | + const message = getMessage(key, args, error) |
| 227 | + Object.defineProperty(error, 'message', { |
| 228 | + value: message, |
| 229 | + enumerable: false, |
| 230 | + writable: true, |
| 231 | + configurable: true |
| 232 | + }) |
| 233 | + Object.defineProperty(error, 'toString', { |
| 234 | + /** @this {Error} */ |
| 235 | + value () { |
| 236 | + return `${this.name} [${key}]: ${this.message}` |
| 237 | + }, |
| 238 | + enumerable: false, |
| 239 | + writable: true, |
| 240 | + configurable: true |
| 241 | + }) |
| 242 | + addCodeToName(error, Base.name, key) |
| 243 | + // @ts-expect-error It’s a Node error. |
| 244 | + error.code = key |
| 245 | + return error |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +const addCodeToName = hideStackFrames( |
| 250 | + /** |
| 251 | + * @param {Error} error |
| 252 | + * @param {string} name |
| 253 | + * @param {string} code |
| 254 | + * @returns {void} |
| 255 | + */ |
| 256 | + function (error, name, code) { |
| 257 | + // Set the stack |
| 258 | + error = captureLargerStackTrace(error) |
| 259 | + // Add the error code to the name to include it in the stack trace. |
| 260 | + error.name = `${name} [${code}]` |
| 261 | + // Access the stack to generate the error message including the error code |
| 262 | + // from the name. |
| 263 | + error.stack // eslint-disable-line no-unused-expressions |
| 264 | + // Reset the name to the actual name. |
| 265 | + if (name === 'SystemError') { |
| 266 | + Object.defineProperty(error, 'name', { |
| 267 | + value: name, |
| 268 | + enumerable: false, |
| 269 | + writable: true, |
| 270 | + configurable: true |
| 271 | + }) |
| 272 | + } else { |
| 273 | + delete error.name |
| 274 | + } |
| 275 | + } |
| 276 | +) |
| 277 | + |
| 278 | +/** |
| 279 | + * @returns {boolean} |
| 280 | + */ |
| 281 | +function isErrorStackTraceLimitWritable () { |
| 282 | + const desc = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit') |
| 283 | + if (desc === undefined) { |
| 284 | + return Object.isExtensible(Error) |
| 285 | + } |
| 286 | + |
| 287 | + return own.call(desc, 'writable') ? desc.writable : desc.set !== undefined |
| 288 | +} |
| 289 | + |
| 290 | +/** |
| 291 | + * This function removes unnecessary frames from Node.js core errors. |
| 292 | + * @template {(...args: unknown[]) => unknown} T |
| 293 | + * @type {(fn: T) => T} |
| 294 | + */ |
| 295 | +function hideStackFrames (fn) { |
| 296 | + // We rename the functions that will be hidden to cut off the stacktrace |
| 297 | + // at the outermost one |
| 298 | + const hidden = nodeInternalPrefix + fn.name |
| 299 | + Object.defineProperty(fn, 'name', { value: hidden }) |
| 300 | + return fn |
| 301 | +} |
| 302 | + |
| 303 | +const captureLargerStackTrace = hideStackFrames( |
| 304 | + /** |
| 305 | + * @param {Error} error |
| 306 | + * @returns {Error} |
| 307 | + */ |
| 308 | + function (error) { |
| 309 | + const stackTraceLimitIsWritable = isErrorStackTraceLimitWritable() |
| 310 | + if (stackTraceLimitIsWritable) { |
| 311 | + userStackTraceLimit = Error.stackTraceLimit |
| 312 | + Error.stackTraceLimit = Number.POSITIVE_INFINITY |
| 313 | + } |
| 314 | + |
| 315 | + Error.captureStackTrace(error) |
| 316 | + |
| 317 | + // Reset the limit |
| 318 | + if (stackTraceLimitIsWritable) { Error.stackTraceLimit = userStackTraceLimit } |
| 319 | + |
| 320 | + return error |
| 321 | + } |
| 322 | +) |
| 323 | + |
| 324 | +/** |
| 325 | + * @param {string} key |
| 326 | + * @param {unknown[]} args |
| 327 | + * @param {Error} self |
| 328 | + * @returns {string} |
| 329 | + */ |
| 330 | +function getMessage (key, args, self) { |
| 331 | + const message = messages.get(key) |
| 332 | + |
| 333 | + if (typeof message === 'function') { |
| 334 | + assert( |
| 335 | + message.length <= args.length, // Default options do not count. |
| 336 | + `Code: ${key}; The provided arguments length (${args.length}) does not ` + |
| 337 | + `match the required ones (${message.length}).` |
| 338 | + ) |
| 339 | + return Reflect.apply(message, self, args) |
| 340 | + } |
| 341 | + |
| 342 | + const expectedLength = (message.match(/%[dfijoOs]/g) || []).length |
| 343 | + assert( |
| 344 | + expectedLength === args.length, |
| 345 | + `Code: ${key}; The provided arguments length (${args.length}) does not ` + |
| 346 | + `match the required ones (${expectedLength}).` |
| 347 | + ) |
| 348 | + if (args.length === 0) { return message } |
| 349 | + |
| 350 | + args.unshift(message) |
| 351 | + return Reflect.apply(format, null, args) |
| 352 | +} |
0 commit comments