This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Convert filesystem API errors to a consistent type #46
Merged
KernelDeimos
merged 1 commit into
HeyPuter:trunk
from
AtkinsSJ:filesystem-consistent-errors
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (C) 2024 Puter Technologies Inc. | ||
* | ||
* This file is part of Phoenix Shell. | ||
* | ||
* Phoenix Shell is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
export const ErrorCodes = { | ||
EACCES: Symbol.for('EACCES'), | ||
EADDRINUSE: Symbol.for('EADDRINUSE'), | ||
ECONNREFUSED: Symbol.for('ECONNREFUSED'), | ||
ECONNRESET: Symbol.for('ECONNRESET'), | ||
EEXIST: Symbol.for('EEXIST'), | ||
EFBIG: Symbol.for('EFBIG'), | ||
EINVAL: Symbol.for('EINVAL'), | ||
EIO: Symbol.for('EIO'), | ||
EISDIR: Symbol.for('EISDIR'), | ||
EMFILE: Symbol.for('EMFILE'), | ||
ENOENT: Symbol.for('ENOENT'), | ||
ENOSPC: Symbol.for('ENOSPC'), | ||
ENOTDIR: Symbol.for('ENOTDIR'), | ||
ENOTEMPTY: Symbol.for('ENOTEMPTY'), | ||
EPERM: Symbol.for('EPERM'), | ||
EPIPE: Symbol.for('EPIPE'), | ||
ETIMEDOUT: Symbol.for('ETIMEDOUT'), | ||
}; | ||
|
||
export class PosixError extends Error { | ||
// posixErrorCode can be either a string, or one of the ErrorCodes above. | ||
constructor(posixErrorCode, message) { | ||
super(message); | ||
if (typeof posixErrorCode === 'symbol') { | ||
if (ErrorCodes[Symbol.keyFor(posixErrorCode)] !== posixErrorCode) { | ||
throw new Error(`Unrecognized POSIX error code: '${posixErrorCode}'`); | ||
} | ||
this.posixCode = posixErrorCode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default for error classes seems to be |
||
} else { | ||
const code = ErrorCodes[posixErrorCode]; | ||
if (!code) throw new Error(`Unrecognized POSIX error code: '${posixErrorCode}'`); | ||
this.posixCode = code; | ||
} | ||
} | ||
|
||
// | ||
// Helpers for constructing a PosixError when you don't already have an error message. | ||
// | ||
static AccessNotPermitted(path) { | ||
return new PosixError(ErrorCodes.EACCES, `Access not permitted to: '${path}'`); | ||
} | ||
static AddressInUse() { | ||
return new PosixError(ErrorCodes.EADDRINUSE, `Address already in use`); | ||
} | ||
static ConnectionRefused() { | ||
return new PosixError(ErrorCodes.ECONNREFUSED, `Connection refused`); | ||
} | ||
static ConnectionReset() { | ||
return new PosixError(ErrorCodes.ECONNRESET, `Connection reset`); | ||
} | ||
static PathAlreadyExists(path) { | ||
return new PosixError(ErrorCodes.EEXIST, `Path already exists: '${path}'`); | ||
} | ||
static FileTooLarge() { | ||
return new PosixError(ErrorCodes.EFBIG, `File too large`); | ||
} | ||
static InvalidArgument(message) { | ||
return new PosixError(ErrorCodes.EINVAL, message); | ||
} | ||
static IO() { | ||
return new PosixError(ErrorCodes.EIO, `IO error`); | ||
} | ||
static IsDirectory(path) { | ||
return new PosixError(ErrorCodes.EISDIR, `Path is directory: '${path}'`); | ||
} | ||
static TooManyOpenFiles() { | ||
return new PosixError(ErrorCodes.EMFILE, `Too many open files`); | ||
} | ||
static DoesNotExist(path) { | ||
return new PosixError(ErrorCodes.ENOENT, `Path not found: '${path}'`); | ||
} | ||
static NotEnoughSpace() { | ||
return new PosixError(ErrorCodes.ENOSPC, `Not enough space available`); | ||
} | ||
static IsNotDirectory(path) { | ||
return new PosixError(ErrorCodes.ENOTDIR, `Path is not a directory: '${path}'`); | ||
} | ||
static DirectoryIsNotEmpty(path) { | ||
return new PosixError(ErrorCodes.ENOTEMPTY, `Directory is not empty: '${path}'`); | ||
} | ||
static OperationNotPermitted() { | ||
return new PosixError(ErrorCodes.EPERM, 'Operation not permitted'); | ||
} | ||
static BrokenPipe() { | ||
return new PosixError(ErrorCodes.EPIPE, 'Broken pipe'); | ||
} | ||
static TimedOut() { | ||
return new PosixError(ErrorCodes.ETIMEDOUT, 'Connection timed out'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,130 @@ | |
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
import { DestinationIsDirectoryError, DestinationIsNotDirectoryError } from "../definitions"; | ||
import { ErrorCodes, PosixError } from '../PosixError.js'; | ||
|
||
function convertPuterError(e) { | ||
// Handle Puter SDK errors | ||
switch (e.code) { | ||
case 'item_with_same_name_exists': return new PosixError(ErrorCodes.EEXIST, e.message); | ||
case 'cannot_move_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message); | ||
case 'cannot_copy_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message); | ||
case 'cannot_move_to_root': return new PosixError(ErrorCodes.EACCES, e.message); | ||
case 'cannot_copy_to_root': return new PosixError(ErrorCodes.EACCES, e.message); | ||
case 'cannot_write_to_root': return new PosixError(ErrorCodes.EACCES, e.message); | ||
case 'cannot_overwrite_a_directory': return new PosixError(ErrorCodes.EPERM, e.message); | ||
case 'cannot_read_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message); | ||
case 'source_and_dest_are_the_same': return new PosixError(ErrorCodes.EPERM, e.message); | ||
case 'dest_is_not_a_directory': return new PosixError(ErrorCodes.ENOTDIR, e.message); | ||
case 'dest_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message); | ||
case 'source_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message); | ||
case 'subject_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message); | ||
case 'shortcut_target_not_found': return new PosixError(ErrorCodes.ENOENT, e.message); | ||
case 'shortcut_target_is_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message); | ||
case 'shortcut_target_is_a_file': return new PosixError(ErrorCodes.ENOTDIR, e.message); | ||
case 'forbidden': return new PosixError(ErrorCodes.EPERM, e.message); | ||
case 'immutable': return new PosixError(ErrorCodes.EACCES, e.message); | ||
case 'field_empty': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'field_missing': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'xor_field_missing': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'field_only_valid_with_other_field': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'invalid_id': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'field_invalid': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'field_immutable': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'field_too_long': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'field_too_short': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'already_in_use': return new PosixError(ErrorCodes.EINVAL, e.message); // Not sure what this one is | ||
case 'invalid_file_name': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'storage_limit_reached': return new PosixError(ErrorCodes.ENOSPC, e.message); | ||
case 'internal_error': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right | ||
case 'response_timeout': return new PosixError(ErrorCodes.ETIMEDOUT, e.message); | ||
case 'file_too_large': return new PosixError(ErrorCodes.EFBIG, e.message); | ||
case 'thumbnail_too_large': return new PosixError(ErrorCodes.EFBIG, e.message); | ||
case 'upload_failed': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right | ||
case 'missing_expected_metadata': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'overwrite_and_dedupe_exclusive': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'not_empty': return new PosixError(ErrorCodes.ENOTEMPTY, e.message); | ||
|
||
// Write | ||
case 'offset_without_existing_file': return new PosixError(ErrorCodes.ENOENT, e.message); | ||
case 'offset_requires_overwrite': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'offset_requires_stream': return new PosixError(ErrorCodes.EPERM, e.message); | ||
|
||
// Batch | ||
case 'batch_too_many_files': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
case 'batch_missing_file': return new PosixError(ErrorCodes.EINVAL, e.message); | ||
|
||
// Open | ||
case 'no_suitable_app': break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These non-file-related ones are left here for reference. We might want to convert them properly later. As is, they'll just be returned without being modified. |
||
case 'app_does_not_exist': break; | ||
|
||
// Apps | ||
case 'app_name_already_in_use': break; | ||
|
||
// Subdomains | ||
case 'subdomain_limit_reached': break; | ||
case 'subdomain_reserved': break; | ||
|
||
// Users | ||
case 'email_already_in_use': break; | ||
case 'username_already_in_use': break; | ||
case 'too_many_username_changes': break; | ||
case 'token_invalid': break; | ||
|
||
// drivers | ||
case 'interface_not_found': break; | ||
case 'no_implementation_available': break; | ||
case 'method_not_found': break; | ||
case 'missing_required_argument': break; | ||
case 'argument_consolidation_failed': break; | ||
|
||
// SLA | ||
case 'rate_limit_exceeded': break; | ||
case 'monthly_limit_exceeded': break; | ||
case 'server_rate_exceeded': break; | ||
|
||
// auth | ||
case 'token_missing': break; | ||
case 'token_auth_failed': break; | ||
case 'token_unsupported': break; | ||
case 'account_suspended': break; | ||
case 'permission_denied': break; | ||
case 'access_token_empty_permissions': break; | ||
|
||
// Object Mapping | ||
case 'field_not_allowed_for_create': break; | ||
case 'field_required_for_update': break; | ||
case 'entity_not_found': break; | ||
|
||
// Chat | ||
case 'max_tokens_exceeded': break; | ||
} | ||
// Some other kind of error | ||
return e; | ||
} | ||
|
||
// DRY: Almost the same as node/filesystem.js | ||
function wrapAPIs(apis) { | ||
for (const method in apis) { | ||
if (typeof apis[method] !== 'function') { | ||
continue; | ||
} | ||
const original = apis[method]; | ||
apis[method] = async (...args) => { | ||
try { | ||
return await original(...args); | ||
} catch (e) { | ||
throw convertPuterError(e); | ||
} | ||
}; | ||
} | ||
return apis; | ||
} | ||
|
||
export const CreateFilesystemProvider = ({ | ||
puterSDK, | ||
}) => { | ||
return { | ||
return wrapAPIs({ | ||
capabilities: { | ||
'readdir.www': true, | ||
}, | ||
|
@@ -39,7 +157,7 @@ export const CreateFilesystemProvider = ({ | |
const stat = await puterSDK.fs.stat(path); | ||
|
||
if ( stat.is_dir && ! recursive ) { | ||
throw new DestinationIsDirectoryError(path); | ||
throw PosixError.IsDirectory(path); | ||
} | ||
|
||
return await puterSDK.fs.delete(path, { recursive }); | ||
|
@@ -50,7 +168,7 @@ export const CreateFilesystemProvider = ({ | |
const stat = await puterSDK.fs.stat(path); | ||
|
||
if ( ! stat.is_dir ) { | ||
throw new DestinationIsNotDirectoryError(path); | ||
throw PosixError.IsNotDirectory(path); | ||
} | ||
|
||
return await puterSDK.fs.delete(path, { recursive: false }); | ||
|
@@ -111,5 +229,5 @@ export const CreateFilesystemProvider = ({ | |
...(new_name ? { newName: new_name } : {}), | ||
}); | ||
}, | ||
} | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Symbols is nice, but also means that there's a small trap when comparing the error code. If you try
if (e.posixCode === 'ENOENT')
it'll fail. But this might not be an issue in practice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same symbol object needs to be attained, for example
e.posixCode === ErrorCodes.ENOENT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. My thinking is this becomes a trap for users because it'd be easy to forget to do that. But then, JS lets you compare it against literally anything so maybe people will be used to that, and it's not an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, somewhere the type of
posixCode
should be documented but then once all the other comparisons are correct contributors will use those as a reference anyway