Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | /** * Shared Error Handling * * Unified error handling utilities. */ /** * Application error class with error code. */ export class AppError extends Error { constructor( message: string, public readonly code: string, public readonly cause?: Error ) { super(message); this.name = 'AppError'; } /** * Create a AppError from an unknown error. */ static fromUnknown(error: unknown, code: string): AppError { if (error instanceof AppError) { return error; } return new AppError(getErrorMessage(error), code, isError(error) ? error : undefined); } } /** * Network error class. */ export class NetworkError extends AppError { constructor(message: string, public readonly statusCode?: number, cause?: Error) { super(message, 'NETWORK_ERROR', cause); this.name = 'NetworkError'; } } /** * Validation error class. */ export class ValidationError extends AppError { constructor(message: string, public readonly field?: string, cause?: Error) { super(message, 'VALIDATION_ERROR', cause); this.name = 'ValidationError'; } } /** * Authentication error class. */ export class AuthError extends AppError { constructor(message: string = 'Authentication required', cause?: Error) { super(message, 'AUTH_ERROR', cause); this.name = 'AuthError'; } } /** * Type guard for Error. */ export function isError(error: unknown): error is Error { return error instanceof Error; } /** * Get error message from unknown error. */ export function getErrorMessage(error: unknown): string { if (isError(error)) { return error.message; } if (typeof error === 'string') { return error; } return String(error); } /** * Wrap error with code. */ export function wrapError(error: unknown, code: string): AppError { return AppError.fromUnknown(error, code); } /** * Check if error is a specific error class. */ export function isAppError(error: unknown): error is AppError { return error instanceof AppError; } export function isNetworkError(error: unknown): error is NetworkError { return error instanceof NetworkError; } export function isValidationError(error: unknown): error is ValidationError { return error instanceof ValidationError; } export function isAuthError(error: unknown): error is AuthError { return error instanceof AuthError; } |