small tweaks to the file structure, and reorganization of the codebase.
preparations for session manager function implementations
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
export interface ShellError {
|
||||
readonly errorType: string;
|
||||
readonly message: string;
|
||||
readonly timestamp: number;
|
||||
readonly exitCode: number;
|
||||
readonly cause?: {
|
||||
readonly name: string;
|
||||
readonly message: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function createShellError<E extends ShellError>(
|
||||
errorType: E["errorType"],
|
||||
message: string,
|
||||
extraProps: Omit<E, keyof ShellError>,
|
||||
cause?: { name: string; message: string },
|
||||
): E {
|
||||
let exitCode = EXIT_CODE_MAP[errorType] ?? 1;
|
||||
|
||||
// Special handling for signal termination
|
||||
if (errorType === "SIGNAL_TERMINATION") {
|
||||
const signalError = extraProps as unknown as { signalNumber: number };
|
||||
exitCode = 128 + signalError.signalNumber;
|
||||
}
|
||||
|
||||
const base: ShellError = Object.freeze({
|
||||
errorType,
|
||||
message,
|
||||
timestamp: Date.now(),
|
||||
exitCode,
|
||||
...Object.freeze(cause !== undefined && { cause }),
|
||||
});
|
||||
|
||||
return Object.freeze({ ...base, ...extraProps }) as E;
|
||||
}
|
||||
|
||||
const EXIT_CODE_MAP: Record<string, number> = {
|
||||
GenericError: 1,
|
||||
InvalidArguments: 2,
|
||||
|
||||
UsageError: 63,
|
||||
PathResolutionError: 64,
|
||||
DataFormatError: 65,
|
||||
FileNotFound: 66,
|
||||
UserNotFound: 67,
|
||||
LoginError: 68,
|
||||
InternalSoftwareError: 70,
|
||||
OperatingSystemError: 71,
|
||||
SystemFileError: 72,
|
||||
CannotCreateOutput: 73,
|
||||
IoError: 74,
|
||||
TemporaryFailure: 75,
|
||||
TimeoutError: 75, // Same as TEMPFAIL
|
||||
ProtocolError: 76,
|
||||
PermissionDenied: 77,
|
||||
ConfigurationError: 78,
|
||||
CommandNotFound: 127,
|
||||
NotExecutable: 126,
|
||||
// SIGNAL_TERMINATION is handled dynamically (128 + signal)
|
||||
};
|
||||
|
||||
/** Generic catch-all error (Exit 1) */
|
||||
|
||||
// Its actually the ShellError itself - no need for a custom type
|
||||
|
||||
/* export interface GenericError extends ShellError {
|
||||
errorType: "GENERIC_ERROR";
|
||||
} */
|
||||
|
||||
export interface InvalidArgumentsError extends ShellError {
|
||||
errorType: "InvalidArguments";
|
||||
expected: string; // e.g., "Usage: command <file>"
|
||||
actual: string; // e.g., the user's input
|
||||
}
|
||||
|
||||
// Codes 3 - 63 reserved for currently running processes, and their specific error handling.
|
||||
|
||||
/** Command used incorrectly (bad flag, wrong arg count) - maps to EX_USAGE (Exit 64) */
|
||||
export interface UsageError extends ShellError {
|
||||
errorType: "UsageError";
|
||||
usageHint: string; // e.g., "command [-a] [-b] <filename>"
|
||||
}
|
||||
|
||||
// ===================== 2. FILE / I/O ERRORS (Exit: 65, 66, 72, 73, 74) =====================
|
||||
|
||||
/** Input file does not exist or is unreadable (Exit 66 - EX_NOINPUT) */
|
||||
export interface FileNotFoundError extends ShellError {
|
||||
errorType: "FileNotFound";
|
||||
path: string;
|
||||
}
|
||||
|
||||
export interface PathResolutionError extends ShellError {
|
||||
errorType: "PathResolutionError";
|
||||
path: string;
|
||||
}
|
||||
|
||||
/** Input data has invalid format (Exit 65 - EX_DATAERR) */
|
||||
export interface DataFormatError extends ShellError {
|
||||
errorType: "DataFormatError";
|
||||
path?: string; // Optional: which file had bad data
|
||||
line?: number; // Optional: line number if applicable
|
||||
}
|
||||
|
||||
/** System file (e.g., /etc/passwd, /etc/hosts) is broken or missing (Exit 72 - EX_OSFILE) */
|
||||
export interface SystemFileError extends ShellError {
|
||||
errorType: "SystemFileError";
|
||||
path: string;
|
||||
}
|
||||
|
||||
/** Cannot create user-specified output file (Exit 73 - EX_CANTCREAT) */
|
||||
export interface CannotCreateOutputError extends ShellError {
|
||||
errorType: "CannotCreateOutput";
|
||||
path: string;
|
||||
reason: string; // e.g., "Directory does not exist", "Read-only filesystem"
|
||||
}
|
||||
|
||||
/** I/O error while reading/writing a file (Exit 74 - EX_IOERR) */
|
||||
export interface IOError extends ShellError {
|
||||
errorType: "IoError";
|
||||
path: string;
|
||||
operation: "read" | "write" | "seek";
|
||||
}
|
||||
|
||||
// ===================== 3. PERMISSION / USER ERRORS (Exit: 67, 77) =====================
|
||||
|
||||
/** Insufficient permission to access a resource (Exit 77 - EX_NOPERM) */
|
||||
export interface PermissionDeniedError extends ShellError {
|
||||
errorType: "PermissionDenied";
|
||||
resource: string; // File path, port, socket, etc.
|
||||
}
|
||||
|
||||
/** Specified user does not exist (Exit 67 - EX_NOUSER) */
|
||||
export interface UserNotFoundError extends ShellError {
|
||||
errorType: "UserNotFound";
|
||||
username: string;
|
||||
}
|
||||
|
||||
export interface LoginError extends ShellError {
|
||||
errorType: "LoginError";
|
||||
user: number;
|
||||
}
|
||||
|
||||
/** Required service is unavailable (Exit 69 - EX_UNAVAILABLE) */
|
||||
export interface ServiceUnavailableError extends ShellError {
|
||||
errorType: "ServiceUnavailable";
|
||||
service: string; // e.g., "database", "DNS", "SSH"
|
||||
port?: number;
|
||||
}
|
||||
|
||||
// ===================== 5. PROCESS / EXECUTION ERRORS (Exit: 126, 127, 71) =====================
|
||||
|
||||
/** Command not found (Exit 127 - POSIX standard) */
|
||||
export interface CommandNotFoundError extends ShellError {
|
||||
errorType: "CommandNotFound";
|
||||
command: string;
|
||||
}
|
||||
|
||||
/** Command found but not executable (Exit 126 - POSIX standard) */
|
||||
export interface NotExecutableError extends ShellError {
|
||||
errorType: "NotExecutable";
|
||||
path: string;
|
||||
}
|
||||
|
||||
/** Operating system error (fork, pipe, process table full) (Exit 71 - EX_OSERR) */
|
||||
export interface OperatingSystemError extends ShellError {
|
||||
errorType: "OperatingSystemError";
|
||||
syscall: string; // e.g., "fork", "pipe", "execve"
|
||||
errno?: number; // Optional: underlying OS error code
|
||||
}
|
||||
|
||||
// ===================== 6. TIMING / RETRY ERRORS (Exit: 75) =====================
|
||||
|
||||
/** Temporary failure; user should retry (Exit 75 - EX_TEMPFAIL) */
|
||||
export interface TemporaryFailureError extends ShellError {
|
||||
errorType: "TemporaryFailure";
|
||||
retryAfter?: number; // Optional: recommended seconds to wait
|
||||
resource?: string; // Optional: what resource is temporarily busy
|
||||
}
|
||||
|
||||
/** Operation timed out (Often mapped to EX_TEMPFAIL, Exit 75) */
|
||||
export interface TimeoutError extends ShellError {
|
||||
errorType: "TimeoutError";
|
||||
timeoutSeconds: number;
|
||||
operation: string; // e.g., "HTTP request", "Database query"
|
||||
}
|
||||
|
||||
// ===================== 7. INTERNAL / CONFIGURATION ERRORS (Exit: 70, 78) =====================
|
||||
|
||||
/** Internal software error (bug, assertion failed) (Exit 70 - EX_SOFTWARE) */
|
||||
export interface InternalSoftwareError extends ShellError {
|
||||
errorType: "InternalSoftwareError";
|
||||
component: string; // e.g., "parser", "network layer"
|
||||
}
|
||||
|
||||
/** Configuration error in the program or system (Exit 78 - EX_CONFIG) */
|
||||
export interface ConfigurationError extends ShellError {
|
||||
errorType: "ConfigurationError";
|
||||
configFile?: string;
|
||||
key?: string; // Which config key is wrong
|
||||
}
|
||||
|
||||
// ===================== 8. PROTOCOL / REMOTE ERRORS (Exit: 76) =====================
|
||||
|
||||
/** Remote system protocol error (Exit 76 - EX_PROTOCOL) */
|
||||
export interface ProtocolError extends ShellError {
|
||||
errorType: "ProtocolError";
|
||||
remoteHost?: string;
|
||||
protocol: string; // e.g., "HTTP", "SSH", "FTP"
|
||||
statusCode?: number; // Optional: e.g., HTTP 400/500
|
||||
}
|
||||
|
||||
// ===================== 9. SIGNAL TERMINATION (Exit: 128 + signal) =====================
|
||||
|
||||
/** Process was killed by a signal (Exit = 128 + signalNumber) */
|
||||
export interface SignalTerminationError extends ShellError {
|
||||
errorType: "SignalTermination";
|
||||
signalNumber: number; // e.g., 9 (SIGKILL), 15 (SIGTERM), 2 (SIGINT)
|
||||
signalName: string; // e.g., "SIGTERM", "SIGKILL"
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||
import type { FileSystem } from "../filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
||||
|
||||
export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
const isAbsolute = path.startsWith("/");
|
||||
|
||||
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
||||
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
||||
|
||||
if (pathComponents.length === 0 && isAbsolute) return 0;
|
||||
if (pathComponents.length === 0) return cwd;
|
||||
|
||||
let currentNode = isAbsolute ? fs.getRoot() : (fs.getNode(cwd) as DirectoryNode);
|
||||
if (!currentNode) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `CWD node under inode ${cwd} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: cwd,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
for (const name of pathComponents) {
|
||||
if (name === ".") continue;
|
||||
|
||||
if (name === "..") {
|
||||
currentNode = fs.getNode(currentNode.parent) as DirectoryNode;
|
||||
continue;
|
||||
}
|
||||
|
||||
const inode = currentNode.children[name];
|
||||
|
||||
if (!inode)
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Child node in ${currentNode.name} under name ${name} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: currentNode.inode,
|
||||
} satisfies NodeNotFoundError;
|
||||
if (fs.isFile(inode))
|
||||
throw {
|
||||
type: "NotADirectory",
|
||||
message: `Child node ${name} is not a directory`,
|
||||
timestamp: Date.now(),
|
||||
inode: inode,
|
||||
nodeType: TreeNodeType.File,
|
||||
} satisfies NotADirectoryError;
|
||||
|
||||
currentNode = fs.getNode(inode) as DirectoryNode;
|
||||
}
|
||||
|
||||
return currentNode.inode;
|
||||
}
|
||||
|
||||
export function resolveInode(fs: FileSystem, inode: number): string {
|
||||
const node = fs.getNode(inode);
|
||||
const components: string[] = [];
|
||||
|
||||
if (node === undefined) return "";
|
||||
|
||||
let currentInode = inode;
|
||||
while (currentInode !== 0) {
|
||||
const node = fs.getNode(currentInode) as TreeNode;
|
||||
if (!node) break;
|
||||
|
||||
components.push(node.name);
|
||||
currentInode = node.parent;
|
||||
}
|
||||
components.reverse();
|
||||
components.unshift("");
|
||||
return components.join("/");
|
||||
}
|
||||
Reference in New Issue
Block a user