-
Notifications
You must be signed in to change notification settings - Fork 15
Convert filesystem API errors to a consistent type #46
Convert filesystem API errors to a consistent type #46
Conversation
The Puter and Node filesystem APIs now convert most errors to a new PosixError class, which holds a standard POSIXy error code. PosixError can be constructed manually, or by using one of the PosixError.Foo() helper functions which produce a default error message.
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 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.
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'), | ||
}; |
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
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 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.
An extra thought about this: For |
The Puter and Node filesystem APIs now convert most errors to a new PosixError class, which holds a standard POSIXy error code. PosixError can be constructed manually, or by using one of the PosixError.Foo() helper functions which produce a default error message.