Skip to content
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
53 changes: 53 additions & 0 deletions sdk/typescript/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* POSIX-style error codes for filesystem operations.
*/
export type FsErrorCode =
| 'ENOENT' // No such file or directory
| 'EEXIST' // File already exists
| 'EISDIR' // Is a directory (when file expected)
| 'ENOTDIR' // Not a directory (when directory expected)
| 'ENOTEMPTY' // Directory not empty
| 'EPERM' // Operation not permitted
| 'EINVAL' // Invalid argument
| 'ENOSYS'; // Function not implemented (use for symlinks)

/**
* Filesystem syscall names for error reporting.
* rm, scandir and copyFile are not actual syscall but used for convenience
*/
export type FsSyscall =
| 'open'
| 'stat'
| 'mkdir'
| 'rmdir'
| 'rm'
| 'unlink'
| 'rename'
| 'scandir'
| 'copyfile'
| 'access';

export interface ErrnoException extends Error {
code?: FsErrorCode;
syscall?: FsSyscall;
path?: string;
}

export function createFsError(params: {
code: FsErrorCode;
syscall: FsSyscall;
path?: string;
message?: string;
}): ErrnoException {
const { code, syscall, path, message } = params;
const base = message ?? code;
const suffix =
path !== undefined
? ` '${path}'`
: '';
const err = new Error(`${code}: ${base}, ${syscall}${suffix}`) as ErrnoException;
err.code = code;
err.syscall = syscall;
if (path !== undefined) err.path = path;
return err;
}
Loading