225 lines
7.2 KiB
TypeScript
225 lines
7.2 KiB
TypeScript
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 === "SignalTermination") {
|
|
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,
|
|
|
|
ParseError: 62,
|
|
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 - 62 reserved for currently running processes, and their specific error handling.
|
|
|
|
export interface ParseError extends ShellError {
|
|
errorType: "ParseError";
|
|
}
|
|
|
|
/** 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"
|
|
}
|