|
| 1 | +'use strict' |
| 2 | +const { inspect } = require('util') |
| 3 | + |
| 4 | +// adapted from node's internal/errors |
| 5 | +// https://github.com/nodejs/node/blob/c8a04049/lib/internal/errors.js |
| 6 | + |
| 7 | +// close copy of node's internal SystemError class. |
| 8 | +class SystemError { |
| 9 | + constructor (code, prefix, context) { |
| 10 | + // XXX context.code is undefined in all constructors used in cp/polyfill |
| 11 | + // that may be a bug copied from node, maybe the constructor should use |
| 12 | + // `code` not `errno`? nodejs/node#41104 |
| 13 | + let message = `${prefix}: ${context.syscall} returned ` + |
| 14 | + `${context.code} (${context.message})` |
| 15 | + |
| 16 | + if (context.path !== undefined) { |
| 17 | + message += ` ${context.path}` |
| 18 | + } |
| 19 | + if (context.dest !== undefined) { |
| 20 | + message += ` => ${context.dest}` |
| 21 | + } |
| 22 | + |
| 23 | + this.code = code |
| 24 | + Object.defineProperties(this, { |
| 25 | + name: { |
| 26 | + value: 'SystemError', |
| 27 | + enumerable: false, |
| 28 | + writable: true, |
| 29 | + configurable: true, |
| 30 | + }, |
| 31 | + message: { |
| 32 | + value: message, |
| 33 | + enumerable: false, |
| 34 | + writable: true, |
| 35 | + configurable: true, |
| 36 | + }, |
| 37 | + info: { |
| 38 | + value: context, |
| 39 | + enumerable: true, |
| 40 | + configurable: true, |
| 41 | + writable: false, |
| 42 | + }, |
| 43 | + errno: { |
| 44 | + get () { |
| 45 | + return context.errno |
| 46 | + }, |
| 47 | + set (value) { |
| 48 | + context.errno = value |
| 49 | + }, |
| 50 | + enumerable: true, |
| 51 | + configurable: true, |
| 52 | + }, |
| 53 | + syscall: { |
| 54 | + get () { |
| 55 | + return context.syscall |
| 56 | + }, |
| 57 | + set (value) { |
| 58 | + context.syscall = value |
| 59 | + }, |
| 60 | + enumerable: true, |
| 61 | + configurable: true, |
| 62 | + }, |
| 63 | + }) |
| 64 | + |
| 65 | + if (context.path !== undefined) { |
| 66 | + Object.defineProperty(this, 'path', { |
| 67 | + get () { |
| 68 | + return context.path |
| 69 | + }, |
| 70 | + set (value) { |
| 71 | + context.path = value |
| 72 | + }, |
| 73 | + enumerable: true, |
| 74 | + configurable: true, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + if (context.dest !== undefined) { |
| 79 | + Object.defineProperty(this, 'dest', { |
| 80 | + get () { |
| 81 | + return context.dest |
| 82 | + }, |
| 83 | + set (value) { |
| 84 | + context.dest = value |
| 85 | + }, |
| 86 | + enumerable: true, |
| 87 | + configurable: true, |
| 88 | + }) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + toString () { |
| 93 | + return `${this.name} [${this.code}]: ${this.message}` |
| 94 | + } |
| 95 | + |
| 96 | + [Symbol.for('nodejs.util.inspect.custom')] (_recurseTimes, ctx) { |
| 97 | + return inspect(this, { |
| 98 | + ...ctx, |
| 99 | + getters: true, |
| 100 | + customInspect: false, |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +function E (code, message) { |
| 106 | + module.exports[code] = class NodeError extends SystemError { |
| 107 | + constructor (ctx) { |
| 108 | + super(code, message, ctx) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +E('ERR_FS_CP_DIR_TO_NON_DIR', 'Cannot overwrite directory with non-directory') |
| 114 | +E('ERR_FS_CP_EEXIST', 'Target already exists') |
| 115 | +E('ERR_FS_CP_EINVAL', 'Invalid src or dest') |
| 116 | +E('ERR_FS_CP_FIFO_PIPE', 'Cannot copy a FIFO pipe') |
| 117 | +E('ERR_FS_CP_NON_DIR_TO_DIR', 'Cannot overwrite non-directory with directory') |
| 118 | +E('ERR_FS_CP_SOCKET', 'Cannot copy a socket file') |
| 119 | +E('ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY', 'Cannot overwrite symlink in subdirectory of self') |
| 120 | +E('ERR_FS_CP_UNKNOWN', 'Cannot copy an unknown file type') |
| 121 | +E('ERR_FS_EISDIR', 'Path is a directory') |
| 122 | + |
| 123 | +module.exports.ERR_INVALID_ARG_TYPE = class ERR_INVALID_ARG_TYPE extends Error { |
| 124 | + constructor (name, expected, actual) { |
| 125 | + super() |
| 126 | + this.code = 'ERR_INVALID_ARG_TYPE' |
| 127 | + this.message = `The ${name} argument must be ${expected}. Received ${typeof actual}` |
| 128 | + } |
| 129 | +} |
0 commit comments