PosixError.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const ErrorCodes = {
  20. EACCES: Symbol.for('EACCES'),
  21. EADDRINUSE: Symbol.for('EADDRINUSE'),
  22. ECONNREFUSED: Symbol.for('ECONNREFUSED'),
  23. ECONNRESET: Symbol.for('ECONNRESET'),
  24. EEXIST: Symbol.for('EEXIST'),
  25. EFBIG: Symbol.for('EFBIG'),
  26. EINVAL: Symbol.for('EINVAL'),
  27. EIO: Symbol.for('EIO'),
  28. EISDIR: Symbol.for('EISDIR'),
  29. EMFILE: Symbol.for('EMFILE'),
  30. ENOENT: Symbol.for('ENOENT'),
  31. ENOSPC: Symbol.for('ENOSPC'),
  32. ENOTDIR: Symbol.for('ENOTDIR'),
  33. ENOTEMPTY: Symbol.for('ENOTEMPTY'),
  34. EPERM: Symbol.for('EPERM'),
  35. EPIPE: Symbol.for('EPIPE'),
  36. ETIMEDOUT: Symbol.for('ETIMEDOUT'),
  37. };
  38. // Codes taken from `errno` on Linux.
  39. const ErrorMetadata = new Map([
  40. [ErrorCodes.EPERM, { code: 1, description: 'Operation not permitted' }],
  41. [ErrorCodes.ENOENT, { code: 2, description: 'File or directory not found' }],
  42. [ErrorCodes.EIO, { code: 5, description: 'IO error' }],
  43. [ErrorCodes.EACCES, { code: 13, description: 'Permission denied' }],
  44. [ErrorCodes.EEXIST, { code: 17, description: 'File already exists' }],
  45. [ErrorCodes.ENOTDIR, { code: 20, description: 'Is not a directory' }],
  46. [ErrorCodes.EISDIR, { code: 21, description: 'Is a directory' }],
  47. [ErrorCodes.EINVAL, { code: 22, description: 'Argument invalid' }],
  48. [ErrorCodes.EMFILE, { code: 24, description: 'Too many open files' }],
  49. [ErrorCodes.EFBIG, { code: 27, description: 'File too big' }],
  50. [ErrorCodes.ENOSPC, { code: 28, description: 'Device out of space' }],
  51. [ErrorCodes.EPIPE, { code: 32, description: 'Pipe broken' }],
  52. [ErrorCodes.ENOTEMPTY, { code: 39, description: 'Directory is not empty' }],
  53. [ErrorCodes.EADDRINUSE, { code: 98, description: 'Address already in use' }],
  54. [ErrorCodes.ECONNRESET, { code: 104, description: 'Connection reset'}],
  55. [ErrorCodes.ETIMEDOUT, { code: 110, description: 'Connection timed out' }],
  56. [ErrorCodes.ECONNREFUSED, { code: 111, description: 'Connection refused' }],
  57. ]);
  58. const errorFromIntegerCode = (code) => {
  59. for (const [errorCode, metadata] of ErrorMetadata) {
  60. if (metadata.code === code) {
  61. return errorCode;
  62. }
  63. }
  64. return undefined;
  65. };
  66. class PosixError extends Error {
  67. // posixErrorCode can be either a string, or one of the ErrorCodes above.
  68. // If message is undefined, a default message will be used.
  69. constructor(posixErrorCode, message) {
  70. let posixCode;
  71. if (typeof posixErrorCode === 'symbol') {
  72. if (ErrorCodes[Symbol.keyFor(posixErrorCode)] !== posixErrorCode) {
  73. throw new Error(`Unrecognized POSIX error code: '${posixErrorCode}'`);
  74. }
  75. posixCode = posixErrorCode;
  76. } else {
  77. const code = ErrorCodes[posixErrorCode];
  78. if (!code) throw new Error(`Unrecognized POSIX error code: '${posixErrorCode}'`);
  79. posixCode = code;
  80. }
  81. super(message ?? ErrorMetadata.get(posixCode).description);
  82. this.posixCode = posixCode;
  83. }
  84. //
  85. // Helpers for constructing a PosixError when you don't already have an error message.
  86. //
  87. static AccessNotPermitted({ message, path } = {}) {
  88. return new PosixError(ErrorCodes.EACCES, message ?? (path ? `Access not permitted to: '${path}'` : undefined));
  89. }
  90. static AddressInUse({ message, address } = {}) {
  91. return new PosixError(ErrorCodes.EADDRINUSE, message ?? (address ? `Address '${address}' in use` : undefined));
  92. }
  93. static ConnectionRefused({ message } = {}) {
  94. return new PosixError(ErrorCodes.ECONNREFUSED, message);
  95. }
  96. static ConnectionReset({ message } = {}) {
  97. return new PosixError(ErrorCodes.ECONNRESET, message);
  98. }
  99. static PathAlreadyExists({ message, path } = {}) {
  100. return new PosixError(ErrorCodes.EEXIST, message ?? (path ? `Path already exists: '${path}'` : undefined));
  101. }
  102. static FileTooLarge({ message } = {}) {
  103. return new PosixError(ErrorCodes.EFBIG, message);
  104. }
  105. static InvalidArgument({ message } = {}) {
  106. return new PosixError(ErrorCodes.EINVAL, message);
  107. }
  108. static IO({ message } = {}) {
  109. return new PosixError(ErrorCodes.EIO, message);
  110. }
  111. static IsDirectory({ message, path } = {}) {
  112. return new PosixError(ErrorCodes.EISDIR, message ?? (path ? `Path is directory: '${path}'` : undefined));
  113. }
  114. static TooManyOpenFiles({ message } = {}) {
  115. return new PosixError(ErrorCodes.EMFILE, message);
  116. }
  117. static DoesNotExist({ message, path } = {}) {
  118. return new PosixError(ErrorCodes.ENOENT, message ?? (path ? `Path not found: '${path}'` : undefined));
  119. }
  120. static NotEnoughSpace({ message } = {}) {
  121. return new PosixError(ErrorCodes.ENOSPC, message);
  122. }
  123. static IsNotDirectory({ message, path } = {}) {
  124. return new PosixError(ErrorCodes.ENOTDIR, message ?? (path ? `Path is not a directory: '${path}'` : undefined));
  125. }
  126. static DirectoryIsNotEmpty({ message, path } = {}) {
  127. return new PosixError(ErrorCodes.ENOTEMPTY, message ?? (path ?`Directory is not empty: '${path}'` : undefined));
  128. }
  129. static OperationNotPermitted({ message } = {}) {
  130. return new PosixError(ErrorCodes.EPERM, message);
  131. }
  132. static BrokenPipe({ message } = {}) {
  133. return new PosixError(ErrorCodes.EPIPE, message);
  134. }
  135. static TimedOut({ message } = {}) {
  136. return new PosixError(ErrorCodes.ETIMEDOUT, message);
  137. }
  138. }
  139. module.exports = {
  140. ErrorCodes,
  141. ErrorMetadata,
  142. errorFromIntegerCode,
  143. PosixError,
  144. }