errors.js 3.3 KB

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