Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Convert filesystem API errors to a consistent type #46

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions src/platform/PosixError.js
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'),
};
Comment on lines +19 to +37
Copy link
Contributor Author

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Collaborator

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


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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default for error classes seems to be .code for the error code, would that make more sense? I wasn't sure whether to make it distinct or not.

} 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');
}
}
31 changes: 0 additions & 31 deletions src/platform/definitions.js

This file was deleted.

53 changes: 47 additions & 6 deletions src/platform/node/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,51 @@ import fs from 'fs';
import path_ from 'path';

import modeString from 'fs-mode-to-string';
import { DestinationIsDirectoryError, DestinationIsNotDirectoryError } from "../definitions.js";
import { ErrorCodes, PosixError } from '../PosixError.js';

function convertNodeError(e) {
switch (e.code) {
case 'EACCES': return new PosixError(ErrorCodes.EACCES, e.message);
case 'EADDRINUSE': return new PosixError(ErrorCodes.EADDRINUSE, e.message);
case 'ECONNREFUSED': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
case 'ECONNRESET': return new PosixError(ErrorCodes.ECONNRESET, e.message);
case 'EEXIST': return new PosixError(ErrorCodes.EEXIST, e.message);
case 'EIO': return new PosixError(ErrorCodes.EIO, e.message);
case 'EISDIR': return new PosixError(ErrorCodes.EISDIR, e.message);
case 'EMFILE': return new PosixError(ErrorCodes.EMFILE, e.message);
case 'ENOENT': return new PosixError(ErrorCodes.ENOENT, e.message);
case 'ENOTDIR': return new PosixError(ErrorCodes.ENOTDIR, e.message);
case 'ENOTEMPTY': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
// ENOTFOUND is Node-specific. ECONNREFUSED is similar enough.
case 'ENOTFOUND': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
case 'EPERM': return new PosixError(ErrorCodes.EPERM, e.message);
case 'EPIPE': return new PosixError(ErrorCodes.EPIPE, e.message);
case 'ETIMEDOUT': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
}
// Some other kind of error
return e;
}

// DRY: Almost the same as puter/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 convertNodeError(e);
}
};
}
return apis;
}

export const CreateFilesystemProvider = () => {
return {
return wrapAPIs({
capabilities: {
'readdir.posix-mode': true,
},
Expand Down Expand Up @@ -116,7 +157,7 @@ export const CreateFilesystemProvider = () => {
const stat = await fs.promises.stat(path);

if ( stat.isDirectory() && ! recursive ) {
throw new DestinationIsDirectoryError(path);
throw PosixError.IsDirectory(path);
}

return await fs.promises.rm(path, { recursive });
Expand All @@ -125,7 +166,7 @@ export const CreateFilesystemProvider = () => {
const stat = await fs.promises.stat(path);

if ( !stat.isDirectory() ) {
throw new DestinationIsNotDirectoryError(path);
throw PosixError.IsNotDirectory(path);
}

return await fs.promises.rmdir(path);
Expand Down Expand Up @@ -163,7 +204,7 @@ export const CreateFilesystemProvider = () => {

// `dir -> file`: invalid
if ( srcIsDir && destStat && ! destStat.isDirectory() ) {
throw Error('Cannot copy a directory into a file');
throw new PosixError(ErrorCodes.ENOTDIR, 'Cannot copy a directory into a file');
}

// `file -> dir`: fs.promises.cp() expects the new path to include the filename.
Expand All @@ -174,5 +215,5 @@ export const CreateFilesystemProvider = () => {

return await fs.promises.cp(oldPath, newPath, { recursive: srcIsDir });
}
};
});
};
128 changes: 123 additions & 5 deletions src/platform/puter/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
},
Expand All @@ -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 });
Expand All @@ -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 });
Expand Down Expand Up @@ -111,5 +229,5 @@ export const CreateFilesystemProvider = ({
...(new_name ? { newName: new_name } : {}),
});
},
}
});
};
Loading