PosixError.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // For when we need to convert errors that we don't recognise
  38. EUNKNOWN: Symbol.for('EUNKNOWN'),
  39. };
  40. // Codes taken from `errno` on Linux.
  41. const ErrorMetadata = new Map([
  42. [ErrorCodes.EPERM, { code: 1, description: 'Operation not permitted' }],
  43. [ErrorCodes.ENOENT, { code: 2, description: 'File or directory not found' }],
  44. [ErrorCodes.EIO, { code: 5, description: 'IO error' }],
  45. [ErrorCodes.EACCES, { code: 13, description: 'Permission denied' }],
  46. [ErrorCodes.EEXIST, { code: 17, description: 'File already exists' }],
  47. [ErrorCodes.ENOTDIR, { code: 20, description: 'Is not a directory' }],
  48. [ErrorCodes.EISDIR, { code: 21, description: 'Is a directory' }],
  49. [ErrorCodes.EINVAL, { code: 22, description: 'Argument invalid' }],
  50. [ErrorCodes.EMFILE, { code: 24, description: 'Too many open files' }],
  51. [ErrorCodes.EFBIG, { code: 27, description: 'File too big' }],
  52. [ErrorCodes.ENOSPC, { code: 28, description: 'Device out of space' }],
  53. [ErrorCodes.EPIPE, { code: 32, description: 'Pipe broken' }],
  54. [ErrorCodes.ENOTEMPTY, { code: 39, description: 'Directory is not empty' }],
  55. [ErrorCodes.EADDRINUSE, { code: 98, description: 'Address already in use' }],
  56. [ErrorCodes.ECONNRESET, { code: 104, description: 'Connection reset'}],
  57. [ErrorCodes.ETIMEDOUT, { code: 110, description: 'Connection timed out' }],
  58. [ErrorCodes.ECONNREFUSED, { code: 111, description: 'Connection refused' }],
  59. [ErrorCodes.EUNKNOWN, { code: -1, description: 'Unknown error' }],
  60. ]);
  61. const errorFromIntegerCode = (code) => {
  62. for (const [errorCode, metadata] of ErrorMetadata) {
  63. if (metadata.code === code) {
  64. return errorCode;
  65. }
  66. }
  67. return undefined;
  68. };
  69. class PosixError extends Error {
  70. // posixErrorCode can be either a string, or one of the ErrorCodes above.
  71. // If message is undefined, a default message will be used.
  72. constructor(posixErrorCode, message) {
  73. let posixCode;
  74. if (typeof posixErrorCode === 'symbol') {
  75. if (ErrorCodes[Symbol.keyFor(posixErrorCode)] !== posixErrorCode) {
  76. throw new Error(`Unrecognized POSIX error code: '${posixErrorCode}'`);
  77. }
  78. posixCode = posixErrorCode;
  79. } else {
  80. const code = ErrorCodes[posixErrorCode];
  81. if (!code) throw new Error(`Unrecognized POSIX error code: '${posixErrorCode}'`);
  82. posixCode = code;
  83. }
  84. super(message ?? ErrorMetadata.get(posixCode).description);
  85. this.posixCode = posixCode;
  86. this.code = posixCode.description;
  87. }
  88. static fromNodeJSError(e) {
  89. switch (e.code) {
  90. case 'EACCES': return new PosixError(ErrorCodes.EACCES, e.message);
  91. case 'EADDRINUSE': return new PosixError(ErrorCodes.EADDRINUSE, e.message);
  92. case 'ECONNREFUSED': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
  93. case 'ECONNRESET': return new PosixError(ErrorCodes.ECONNRESET, e.message);
  94. case 'EEXIST': return new PosixError(ErrorCodes.EEXIST, e.message);
  95. case 'EIO': return new PosixError(ErrorCodes.EIO, e.message);
  96. case 'EISDIR': return new PosixError(ErrorCodes.EISDIR, e.message);
  97. case 'EMFILE': return new PosixError(ErrorCodes.EMFILE, e.message);
  98. case 'ENOENT': return new PosixError(ErrorCodes.ENOENT, e.message);
  99. case 'ENOTDIR': return new PosixError(ErrorCodes.ENOTDIR, e.message);
  100. case 'ENOTEMPTY': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
  101. // ENOTFOUND is Node-specific. ECONNREFUSED is similar enough.
  102. case 'ENOTFOUND': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
  103. case 'EPERM': return new PosixError(ErrorCodes.EPERM, e.message);
  104. case 'EPIPE': return new PosixError(ErrorCodes.EPIPE, e.message);
  105. case 'ETIMEDOUT': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
  106. }
  107. // Some other kind of error
  108. return new PosixError(ErrorCodes.EUNKNOWN, e.message);
  109. }
  110. static fromPuterAPIError(e) {
  111. // Handle Puter SDK errors
  112. switch (e.code) {
  113. case 'item_with_same_name_exists': return new PosixError(ErrorCodes.EEXIST, e.message);
  114. case 'cannot_move_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message);
  115. case 'cannot_copy_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message);
  116. case 'cannot_move_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
  117. case 'cannot_copy_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
  118. case 'cannot_write_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
  119. case 'cannot_overwrite_a_directory': return new PosixError(ErrorCodes.EPERM, e.message);
  120. case 'cannot_read_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message);
  121. case 'source_and_dest_are_the_same': return new PosixError(ErrorCodes.EPERM, e.message);
  122. case 'dest_is_not_a_directory': return new PosixError(ErrorCodes.ENOTDIR, e.message);
  123. case 'dest_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
  124. case 'source_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
  125. case 'subject_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
  126. case 'shortcut_target_not_found': return new PosixError(ErrorCodes.ENOENT, e.message);
  127. case 'shortcut_target_is_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message);
  128. case 'shortcut_target_is_a_file': return new PosixError(ErrorCodes.ENOTDIR, e.message);
  129. case 'forbidden': return new PosixError(ErrorCodes.EPERM, e.message);
  130. case 'immutable': return new PosixError(ErrorCodes.EACCES, e.message);
  131. case 'field_empty': return new PosixError(ErrorCodes.EINVAL, e.message);
  132. case 'field_missing': return new PosixError(ErrorCodes.EINVAL, e.message);
  133. case 'xor_field_missing': return new PosixError(ErrorCodes.EINVAL, e.message);
  134. case 'field_only_valid_with_other_field': return new PosixError(ErrorCodes.EINVAL, e.message);
  135. case 'invalid_id': return new PosixError(ErrorCodes.EINVAL, e.message);
  136. case 'field_invalid': return new PosixError(ErrorCodes.EINVAL, e.message);
  137. case 'field_immutable': return new PosixError(ErrorCodes.EINVAL, e.message);
  138. case 'field_too_long': return new PosixError(ErrorCodes.EINVAL, e.message);
  139. case 'field_too_short': return new PosixError(ErrorCodes.EINVAL, e.message);
  140. case 'already_in_use': return new PosixError(ErrorCodes.EINVAL, e.message); // Not sure what this one is
  141. case 'invalid_file_name': return new PosixError(ErrorCodes.EINVAL, e.message);
  142. case 'storage_limit_reached': return new PosixError(ErrorCodes.ENOSPC, e.message);
  143. case 'internal_error': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right
  144. case 'response_timeout': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
  145. case 'file_too_large': return new PosixError(ErrorCodes.EFBIG, e.message);
  146. case 'thumbnail_too_large': return new PosixError(ErrorCodes.EFBIG, e.message);
  147. case 'upload_failed': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right
  148. case 'missing_expected_metadata': return new PosixError(ErrorCodes.EINVAL, e.message);
  149. case 'overwrite_and_dedupe_exclusive': return new PosixError(ErrorCodes.EINVAL, e.message);
  150. case 'not_empty': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
  151. // Write
  152. case 'offset_without_existing_file': return new PosixError(ErrorCodes.ENOENT, e.message);
  153. case 'offset_requires_overwrite': return new PosixError(ErrorCodes.EINVAL, e.message);
  154. case 'offset_requires_stream': return new PosixError(ErrorCodes.EPERM, e.message);
  155. // Batch
  156. case 'batch_too_many_files': return new PosixError(ErrorCodes.EINVAL, e.message);
  157. case 'batch_missing_file': return new PosixError(ErrorCodes.EINVAL, e.message);
  158. // TODO: Associate more of these with posix error codes
  159. // Open
  160. case 'no_suitable_app': break;
  161. case 'app_does_not_exist': break;
  162. // Apps
  163. case 'app_name_already_in_use': break;
  164. // Subdomains
  165. case 'subdomain_limit_reached': break;
  166. case 'subdomain_reserved': break;
  167. // Users
  168. case 'email_already_in_use': break;
  169. case 'username_already_in_use': break;
  170. case 'too_many_username_changes': break;
  171. case 'token_invalid': break;
  172. // drivers
  173. case 'interface_not_found': break;
  174. case 'no_implementation_available': break;
  175. case 'method_not_found': break;
  176. case 'missing_required_argument': break;
  177. case 'argument_consolidation_failed': break;
  178. // SLA
  179. case 'rate_limit_exceeded': break;
  180. case 'monthly_limit_exceeded': break;
  181. case 'server_rate_exceeded': break;
  182. // auth
  183. case 'token_missing': break;
  184. case 'token_auth_failed': break;
  185. case 'token_unsupported': break;
  186. case 'account_suspended': break;
  187. case 'permission_denied': break;
  188. case 'access_token_empty_permissions': break;
  189. // Object Mapping
  190. case 'field_not_allowed_for_create': break;
  191. case 'field_required_for_update': break;
  192. case 'entity_not_found': break;
  193. // Chat
  194. case 'max_tokens_exceeded': break;
  195. }
  196. // Some other kind of error
  197. return new PosixError(ErrorCodes.EUNKNOWN, e.message);
  198. }
  199. //
  200. // Helpers for constructing a PosixError when you don't already have an error message.
  201. //
  202. static AccessNotPermitted({ message, path } = {}) {
  203. return new PosixError(ErrorCodes.EACCES, message ?? (path ? `Access not permitted to: '${path}'` : undefined));
  204. }
  205. static AddressInUse({ message, address } = {}) {
  206. return new PosixError(ErrorCodes.EADDRINUSE, message ?? (address ? `Address '${address}' in use` : undefined));
  207. }
  208. static ConnectionRefused({ message } = {}) {
  209. return new PosixError(ErrorCodes.ECONNREFUSED, message);
  210. }
  211. static ConnectionReset({ message } = {}) {
  212. return new PosixError(ErrorCodes.ECONNRESET, message);
  213. }
  214. static PathAlreadyExists({ message, path } = {}) {
  215. return new PosixError(ErrorCodes.EEXIST, message ?? (path ? `Path already exists: '${path}'` : undefined));
  216. }
  217. static FileTooLarge({ message } = {}) {
  218. return new PosixError(ErrorCodes.EFBIG, message);
  219. }
  220. static InvalidArgument({ message } = {}) {
  221. return new PosixError(ErrorCodes.EINVAL, message);
  222. }
  223. static IO({ message } = {}) {
  224. return new PosixError(ErrorCodes.EIO, message);
  225. }
  226. static IsDirectory({ message, path } = {}) {
  227. return new PosixError(ErrorCodes.EISDIR, message ?? (path ? `Path is directory: '${path}'` : undefined));
  228. }
  229. static TooManyOpenFiles({ message } = {}) {
  230. return new PosixError(ErrorCodes.EMFILE, message);
  231. }
  232. static DoesNotExist({ message, path } = {}) {
  233. return new PosixError(ErrorCodes.ENOENT, message ?? (path ? `Path not found: '${path}'` : undefined));
  234. }
  235. static NotEnoughSpace({ message } = {}) {
  236. return new PosixError(ErrorCodes.ENOSPC, message);
  237. }
  238. static IsNotDirectory({ message, path } = {}) {
  239. return new PosixError(ErrorCodes.ENOTDIR, message ?? (path ? `Path is not a directory: '${path}'` : undefined));
  240. }
  241. static DirectoryIsNotEmpty({ message, path } = {}) {
  242. return new PosixError(ErrorCodes.ENOTEMPTY, message ?? (path ?`Directory is not empty: '${path}'` : undefined));
  243. }
  244. static OperationNotPermitted({ message } = {}) {
  245. return new PosixError(ErrorCodes.EPERM, message);
  246. }
  247. static BrokenPipe({ message } = {}) {
  248. return new PosixError(ErrorCodes.EPIPE, message);
  249. }
  250. static TimedOut({ message } = {}) {
  251. return new PosixError(ErrorCodes.ETIMEDOUT, message);
  252. }
  253. }
  254. module.exports = {
  255. ErrorCodes,
  256. ErrorMetadata,
  257. errorFromIntegerCode,
  258. PosixError,
  259. }