Skip to content

Commit 017077f

Browse files
committed
Move error-conversion functions into PosixError
Specifically, this makes the Puter->PosixError conversion available to the in-progress git client.
1 parent e798576 commit 017077f

File tree

4 files changed

+134
-126
lines changed

4 files changed

+134
-126
lines changed

packages/phoenix/src/platform/node/filesystem.js

+1-24
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,6 @@ import path_ from 'path';
2222
import modeString from 'fs-mode-to-string';
2323
import { ErrorCodes, PosixError } from '@heyputer/puter-js-common/src/PosixError.js';
2424

25-
function convertNodeError(e) {
26-
switch (e.code) {
27-
case 'EACCES': return new PosixError(ErrorCodes.EACCES, e.message);
28-
case 'EADDRINUSE': return new PosixError(ErrorCodes.EADDRINUSE, e.message);
29-
case 'ECONNREFUSED': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
30-
case 'ECONNRESET': return new PosixError(ErrorCodes.ECONNRESET, e.message);
31-
case 'EEXIST': return new PosixError(ErrorCodes.EEXIST, e.message);
32-
case 'EIO': return new PosixError(ErrorCodes.EIO, e.message);
33-
case 'EISDIR': return new PosixError(ErrorCodes.EISDIR, e.message);
34-
case 'EMFILE': return new PosixError(ErrorCodes.EMFILE, e.message);
35-
case 'ENOENT': return new PosixError(ErrorCodes.ENOENT, e.message);
36-
case 'ENOTDIR': return new PosixError(ErrorCodes.ENOTDIR, e.message);
37-
case 'ENOTEMPTY': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
38-
// ENOTFOUND is Node-specific. ECONNREFUSED is similar enough.
39-
case 'ENOTFOUND': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
40-
case 'EPERM': return new PosixError(ErrorCodes.EPERM, e.message);
41-
case 'EPIPE': return new PosixError(ErrorCodes.EPIPE, e.message);
42-
case 'ETIMEDOUT': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
43-
}
44-
// Some other kind of error
45-
return e;
46-
}
47-
4825
// DRY: Almost the same as puter/filesystem.js
4926
function wrapAPIs(apis) {
5027
for (const method in apis) {
@@ -56,7 +33,7 @@ function wrapAPIs(apis) {
5633
try {
5734
return await original(...args);
5835
} catch (e) {
59-
throw convertNodeError(e);
36+
throw PosixError.fromNodeJSError(e);
6037
}
6138
};
6239
}

packages/phoenix/src/platform/puter/filesystem.js

+1-101
Original file line numberDiff line numberDiff line change
@@ -18,106 +18,6 @@
1818
*/
1919
import { ErrorCodes, PosixError } from '@heyputer/puter-js-common/src/PosixError.js';
2020

21-
function convertPuterError(e) {
22-
// Handle Puter SDK errors
23-
switch (e.code) {
24-
case 'item_with_same_name_exists': return new PosixError(ErrorCodes.EEXIST, e.message);
25-
case 'cannot_move_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message);
26-
case 'cannot_copy_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message);
27-
case 'cannot_move_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
28-
case 'cannot_copy_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
29-
case 'cannot_write_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
30-
case 'cannot_overwrite_a_directory': return new PosixError(ErrorCodes.EPERM, e.message);
31-
case 'cannot_read_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message);
32-
case 'source_and_dest_are_the_same': return new PosixError(ErrorCodes.EPERM, e.message);
33-
case 'dest_is_not_a_directory': return new PosixError(ErrorCodes.ENOTDIR, e.message);
34-
case 'dest_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
35-
case 'source_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
36-
case 'subject_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
37-
case 'shortcut_target_not_found': return new PosixError(ErrorCodes.ENOENT, e.message);
38-
case 'shortcut_target_is_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message);
39-
case 'shortcut_target_is_a_file': return new PosixError(ErrorCodes.ENOTDIR, e.message);
40-
case 'forbidden': return new PosixError(ErrorCodes.EPERM, e.message);
41-
case 'immutable': return new PosixError(ErrorCodes.EACCES, e.message);
42-
case 'field_empty': return new PosixError(ErrorCodes.EINVAL, e.message);
43-
case 'field_missing': return new PosixError(ErrorCodes.EINVAL, e.message);
44-
case 'xor_field_missing': return new PosixError(ErrorCodes.EINVAL, e.message);
45-
case 'field_only_valid_with_other_field': return new PosixError(ErrorCodes.EINVAL, e.message);
46-
case 'invalid_id': return new PosixError(ErrorCodes.EINVAL, e.message);
47-
case 'field_invalid': return new PosixError(ErrorCodes.EINVAL, e.message);
48-
case 'field_immutable': return new PosixError(ErrorCodes.EINVAL, e.message);
49-
case 'field_too_long': return new PosixError(ErrorCodes.EINVAL, e.message);
50-
case 'field_too_short': return new PosixError(ErrorCodes.EINVAL, e.message);
51-
case 'already_in_use': return new PosixError(ErrorCodes.EINVAL, e.message); // Not sure what this one is
52-
case 'invalid_file_name': return new PosixError(ErrorCodes.EINVAL, e.message);
53-
case 'storage_limit_reached': return new PosixError(ErrorCodes.ENOSPC, e.message);
54-
case 'internal_error': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right
55-
case 'response_timeout': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
56-
case 'file_too_large': return new PosixError(ErrorCodes.EFBIG, e.message);
57-
case 'thumbnail_too_large': return new PosixError(ErrorCodes.EFBIG, e.message);
58-
case 'upload_failed': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right
59-
case 'missing_expected_metadata': return new PosixError(ErrorCodes.EINVAL, e.message);
60-
case 'overwrite_and_dedupe_exclusive': return new PosixError(ErrorCodes.EINVAL, e.message);
61-
case 'not_empty': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
62-
63-
// Write
64-
case 'offset_without_existing_file': return new PosixError(ErrorCodes.ENOENT, e.message);
65-
case 'offset_requires_overwrite': return new PosixError(ErrorCodes.EINVAL, e.message);
66-
case 'offset_requires_stream': return new PosixError(ErrorCodes.EPERM, e.message);
67-
68-
// Batch
69-
case 'batch_too_many_files': return new PosixError(ErrorCodes.EINVAL, e.message);
70-
case 'batch_missing_file': return new PosixError(ErrorCodes.EINVAL, e.message);
71-
72-
// Open
73-
case 'no_suitable_app': break;
74-
case 'app_does_not_exist': break;
75-
76-
// Apps
77-
case 'app_name_already_in_use': break;
78-
79-
// Subdomains
80-
case 'subdomain_limit_reached': break;
81-
case 'subdomain_reserved': break;
82-
83-
// Users
84-
case 'email_already_in_use': break;
85-
case 'username_already_in_use': break;
86-
case 'too_many_username_changes': break;
87-
case 'token_invalid': break;
88-
89-
// drivers
90-
case 'interface_not_found': break;
91-
case 'no_implementation_available': break;
92-
case 'method_not_found': break;
93-
case 'missing_required_argument': break;
94-
case 'argument_consolidation_failed': break;
95-
96-
// SLA
97-
case 'rate_limit_exceeded': break;
98-
case 'monthly_limit_exceeded': break;
99-
case 'server_rate_exceeded': break;
100-
101-
// auth
102-
case 'token_missing': break;
103-
case 'token_auth_failed': break;
104-
case 'token_unsupported': break;
105-
case 'account_suspended': break;
106-
case 'permission_denied': break;
107-
case 'access_token_empty_permissions': break;
108-
109-
// Object Mapping
110-
case 'field_not_allowed_for_create': break;
111-
case 'field_required_for_update': break;
112-
case 'entity_not_found': break;
113-
114-
// Chat
115-
case 'max_tokens_exceeded': break;
116-
}
117-
// Some other kind of error
118-
return e;
119-
}
120-
12121
// DRY: Almost the same as node/filesystem.js
12222
function wrapAPIs(apis) {
12323
for (const method in apis) {
@@ -129,7 +29,7 @@ function wrapAPIs(apis) {
12929
try {
13030
return await original(...args);
13131
} catch (e) {
132-
throw convertPuterError(e);
32+
throw PosixError.fromPuterAPIError(e);
13333
}
13434
};
13535
}

packages/phoenix/test/coreutils/errno.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export const runErrnoTests = () => {
100100
'EADDRINUSE 98 Address already in use\n' +
101101
'ECONNRESET 104 Connection reset\n' +
102102
'ETIMEDOUT 110 Connection timed out\n' +
103-
'ECONNREFUSED 111 Connection refused\n',
103+
'ECONNREFUSED 111 Connection refused\n' +
104+
'EUNKNOWN -1 Unknown error\n',
104105
expectedStderr: '',
105106
expectedFail: false,
106107
},

packages/puter-js-common/src/PosixError.js

+130
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const ErrorCodes = {
3434
EPERM: Symbol.for('EPERM'),
3535
EPIPE: Symbol.for('EPIPE'),
3636
ETIMEDOUT: Symbol.for('ETIMEDOUT'),
37+
38+
// For when we need to convert errors that we don't recognise
39+
EUNKNOWN: Symbol.for('EUNKNOWN'),
3740
};
3841

3942
// Codes taken from `errno` on Linux.
@@ -55,6 +58,8 @@ const ErrorMetadata = new Map([
5558
[ErrorCodes.ECONNRESET, { code: 104, description: 'Connection reset'}],
5659
[ErrorCodes.ETIMEDOUT, { code: 110, description: 'Connection timed out' }],
5760
[ErrorCodes.ECONNREFUSED, { code: 111, description: 'Connection refused' }],
61+
62+
[ErrorCodes.EUNKNOWN, { code: -1, description: 'Unknown error' }],
5863
]);
5964

6065
const errorFromIntegerCode = (code) => {
@@ -86,6 +91,131 @@ class PosixError extends Error {
8691
this.posixCode = posixCode;
8792
}
8893

94+
static fromNodeJSError(e) {
95+
switch (e.code) {
96+
case 'EACCES': return new PosixError(ErrorCodes.EACCES, e.message);
97+
case 'EADDRINUSE': return new PosixError(ErrorCodes.EADDRINUSE, e.message);
98+
case 'ECONNREFUSED': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
99+
case 'ECONNRESET': return new PosixError(ErrorCodes.ECONNRESET, e.message);
100+
case 'EEXIST': return new PosixError(ErrorCodes.EEXIST, e.message);
101+
case 'EIO': return new PosixError(ErrorCodes.EIO, e.message);
102+
case 'EISDIR': return new PosixError(ErrorCodes.EISDIR, e.message);
103+
case 'EMFILE': return new PosixError(ErrorCodes.EMFILE, e.message);
104+
case 'ENOENT': return new PosixError(ErrorCodes.ENOENT, e.message);
105+
case 'ENOTDIR': return new PosixError(ErrorCodes.ENOTDIR, e.message);
106+
case 'ENOTEMPTY': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
107+
// ENOTFOUND is Node-specific. ECONNREFUSED is similar enough.
108+
case 'ENOTFOUND': return new PosixError(ErrorCodes.ECONNREFUSED, e.message);
109+
case 'EPERM': return new PosixError(ErrorCodes.EPERM, e.message);
110+
case 'EPIPE': return new PosixError(ErrorCodes.EPIPE, e.message);
111+
case 'ETIMEDOUT': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
112+
}
113+
// Some other kind of error
114+
return new PosixError(ErrorCodes.EUNKNOWN, e.message);
115+
}
116+
117+
static fromPuterAPIError(e) {
118+
// Handle Puter SDK errors
119+
switch (e.code) {
120+
case 'item_with_same_name_exists': return new PosixError(ErrorCodes.EEXIST, e.message);
121+
case 'cannot_move_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message);
122+
case 'cannot_copy_item_into_itself': return new PosixError(ErrorCodes.EPERM, e.message);
123+
case 'cannot_move_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
124+
case 'cannot_copy_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
125+
case 'cannot_write_to_root': return new PosixError(ErrorCodes.EACCES, e.message);
126+
case 'cannot_overwrite_a_directory': return new PosixError(ErrorCodes.EPERM, e.message);
127+
case 'cannot_read_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message);
128+
case 'source_and_dest_are_the_same': return new PosixError(ErrorCodes.EPERM, e.message);
129+
case 'dest_is_not_a_directory': return new PosixError(ErrorCodes.ENOTDIR, e.message);
130+
case 'dest_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
131+
case 'source_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
132+
case 'subject_does_not_exist': return new PosixError(ErrorCodes.ENOENT, e.message);
133+
case 'shortcut_target_not_found': return new PosixError(ErrorCodes.ENOENT, e.message);
134+
case 'shortcut_target_is_a_directory': return new PosixError(ErrorCodes.EISDIR, e.message);
135+
case 'shortcut_target_is_a_file': return new PosixError(ErrorCodes.ENOTDIR, e.message);
136+
case 'forbidden': return new PosixError(ErrorCodes.EPERM, e.message);
137+
case 'immutable': return new PosixError(ErrorCodes.EACCES, e.message);
138+
case 'field_empty': return new PosixError(ErrorCodes.EINVAL, e.message);
139+
case 'field_missing': return new PosixError(ErrorCodes.EINVAL, e.message);
140+
case 'xor_field_missing': return new PosixError(ErrorCodes.EINVAL, e.message);
141+
case 'field_only_valid_with_other_field': return new PosixError(ErrorCodes.EINVAL, e.message);
142+
case 'invalid_id': return new PosixError(ErrorCodes.EINVAL, e.message);
143+
case 'field_invalid': return new PosixError(ErrorCodes.EINVAL, e.message);
144+
case 'field_immutable': return new PosixError(ErrorCodes.EINVAL, e.message);
145+
case 'field_too_long': return new PosixError(ErrorCodes.EINVAL, e.message);
146+
case 'field_too_short': return new PosixError(ErrorCodes.EINVAL, e.message);
147+
case 'already_in_use': return new PosixError(ErrorCodes.EINVAL, e.message); // Not sure what this one is
148+
case 'invalid_file_name': return new PosixError(ErrorCodes.EINVAL, e.message);
149+
case 'storage_limit_reached': return new PosixError(ErrorCodes.ENOSPC, e.message);
150+
case 'internal_error': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right
151+
case 'response_timeout': return new PosixError(ErrorCodes.ETIMEDOUT, e.message);
152+
case 'file_too_large': return new PosixError(ErrorCodes.EFBIG, e.message);
153+
case 'thumbnail_too_large': return new PosixError(ErrorCodes.EFBIG, e.message);
154+
case 'upload_failed': return new PosixError(ErrorCodes.ECONNRESET, e.message); // This isn't quite right
155+
case 'missing_expected_metadata': return new PosixError(ErrorCodes.EINVAL, e.message);
156+
case 'overwrite_and_dedupe_exclusive': return new PosixError(ErrorCodes.EINVAL, e.message);
157+
case 'not_empty': return new PosixError(ErrorCodes.ENOTEMPTY, e.message);
158+
159+
// Write
160+
case 'offset_without_existing_file': return new PosixError(ErrorCodes.ENOENT, e.message);
161+
case 'offset_requires_overwrite': return new PosixError(ErrorCodes.EINVAL, e.message);
162+
case 'offset_requires_stream': return new PosixError(ErrorCodes.EPERM, e.message);
163+
164+
// Batch
165+
case 'batch_too_many_files': return new PosixError(ErrorCodes.EINVAL, e.message);
166+
case 'batch_missing_file': return new PosixError(ErrorCodes.EINVAL, e.message);
167+
168+
// TODO: Associate more of these with posix error codes
169+
170+
// Open
171+
case 'no_suitable_app': break;
172+
case 'app_does_not_exist': break;
173+
174+
// Apps
175+
case 'app_name_already_in_use': break;
176+
177+
// Subdomains
178+
case 'subdomain_limit_reached': break;
179+
case 'subdomain_reserved': break;
180+
181+
// Users
182+
case 'email_already_in_use': break;
183+
case 'username_already_in_use': break;
184+
case 'too_many_username_changes': break;
185+
case 'token_invalid': break;
186+
187+
// drivers
188+
case 'interface_not_found': break;
189+
case 'no_implementation_available': break;
190+
case 'method_not_found': break;
191+
case 'missing_required_argument': break;
192+
case 'argument_consolidation_failed': break;
193+
194+
// SLA
195+
case 'rate_limit_exceeded': break;
196+
case 'monthly_limit_exceeded': break;
197+
case 'server_rate_exceeded': break;
198+
199+
// auth
200+
case 'token_missing': break;
201+
case 'token_auth_failed': break;
202+
case 'token_unsupported': break;
203+
case 'account_suspended': break;
204+
case 'permission_denied': break;
205+
case 'access_token_empty_permissions': break;
206+
207+
// Object Mapping
208+
case 'field_not_allowed_for_create': break;
209+
case 'field_required_for_update': break;
210+
case 'entity_not_found': break;
211+
212+
// Chat
213+
case 'max_tokens_exceeded': break;
214+
}
215+
// Some other kind of error
216+
return new PosixError(ErrorCodes.EUNKNOWN, e.message);
217+
}
218+
89219
//
90220
// Helpers for constructing a PosixError when you don't already have an error message.
91221
//

0 commit comments

Comments
 (0)