|
| 1 | +export enum GtfsErrorCategory { |
| 2 | + CONFIG = 'config', |
| 3 | + DOWNLOAD = 'download', |
| 4 | + ZIP = 'zip', |
| 5 | + VALIDATION = 'validation', |
| 6 | + DATABASE = 'database', |
| 7 | + PARSE = 'parse', |
| 8 | + QUERY = 'query', |
| 9 | + INTERNAL = 'internal', |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Error codes are a public API contract and must remain stable across |
| 14 | + * minor/patch releases. |
| 15 | + */ |
| 16 | +export enum GtfsErrorCode { |
| 17 | + GTFS_DOWNLOAD_HTTP = 'GTFS_DOWNLOAD_HTTP', |
| 18 | + GTFS_DOWNLOAD_FAILED = 'GTFS_DOWNLOAD_FAILED', |
| 19 | + GTFS_ZIP_INVALID = 'GTFS_ZIP_INVALID', |
| 20 | + GTFS_REQUIRED_FIELD_MISSING = 'GTFS_REQUIRED_FIELD_MISSING', |
| 21 | + GTFS_INVALID_DATE = 'GTFS_INVALID_DATE', |
| 22 | + GTFS_CONFIG_INVALID = 'GTFS_CONFIG_INVALID', |
| 23 | + DB_OPEN_FAILED = 'DB_OPEN_FAILED', |
| 24 | + GTFS_DB_OPERATION_FAILED = 'GTFS_DB_OPERATION_FAILED', |
| 25 | + GTFS_JSON_INVALID = 'GTFS_JSON_INVALID', |
| 26 | + GTFS_UNSUPPORTED_FILE_TYPE = 'GTFS_UNSUPPORTED_FILE_TYPE', |
| 27 | + GTFS_CSV_PARSE_FAILED = 'GTFS_CSV_PARSE_FAILED', |
| 28 | + GTFS_QUERY_INVALID = 'GTFS_QUERY_INVALID', |
| 29 | +} |
| 30 | + |
| 31 | +export enum GtfsWarningCode { |
| 32 | + GTFS_DUPLICATE_PRIMARY_KEY = 'GTFS_DUPLICATE_PRIMARY_KEY', |
| 33 | +} |
| 34 | + |
| 35 | +export interface GtfsWarning { |
| 36 | + code: GtfsWarningCode; |
| 37 | + message: string; |
| 38 | + details?: Record<string, unknown>; |
| 39 | +} |
| 40 | + |
| 41 | +export interface ImportReport { |
| 42 | + errors: GtfsError[]; |
| 43 | + warnings: GtfsWarning[]; |
| 44 | + errorCountsByCode: Partial<Record<GtfsErrorCode, number>>; |
| 45 | + warningCountsByCode: Partial<Record<GtfsWarningCode, number>>; |
| 46 | +} |
| 47 | + |
| 48 | +interface GtfsErrorOptions { |
| 49 | + code: GtfsErrorCode; |
| 50 | + category: GtfsErrorCategory; |
| 51 | + isOperational?: boolean; |
| 52 | + statusCode?: number; |
| 53 | + details?: Record<string, unknown>; |
| 54 | + cause?: unknown; |
| 55 | +} |
| 56 | + |
| 57 | +export class GtfsError extends Error { |
| 58 | + code: GtfsErrorCode; |
| 59 | + category: GtfsErrorCategory; |
| 60 | + isOperational: boolean; |
| 61 | + statusCode?: number; |
| 62 | + details?: Record<string, unknown>; |
| 63 | + |
| 64 | + constructor(message: string, options: GtfsErrorOptions) { |
| 65 | + super(message, { cause: options.cause }); |
| 66 | + this.name = 'GtfsError'; |
| 67 | + this.code = options.code; |
| 68 | + this.category = options.category; |
| 69 | + this.isOperational = options.isOperational ?? true; |
| 70 | + this.statusCode = options.statusCode; |
| 71 | + this.details = options.details; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export function isGtfsError(error: unknown): error is GtfsError { |
| 76 | + if (!error || typeof error !== 'object') { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + const candidate = error as Partial<GtfsError> & { name?: unknown }; |
| 81 | + return ( |
| 82 | + candidate.name === 'GtfsError' && |
| 83 | + typeof candidate.message === 'string' && |
| 84 | + typeof candidate.code === 'string' && |
| 85 | + typeof candidate.category === 'string' && |
| 86 | + typeof candidate.isOperational === 'boolean' |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +export function isGtfsValidationError(error: unknown): error is GtfsError { |
| 91 | + return isGtfsError(error) && error.category === GtfsErrorCategory.VALIDATION; |
| 92 | +} |
| 93 | + |
| 94 | +export function toGtfsError( |
| 95 | + error: unknown, |
| 96 | + fallback: Omit<GtfsErrorOptions, 'cause'> & { message: string }, |
| 97 | +): GtfsError { |
| 98 | + if (isGtfsError(error)) { |
| 99 | + return error; |
| 100 | + } |
| 101 | + |
| 102 | + return new GtfsError(fallback.message, { |
| 103 | + ...fallback, |
| 104 | + cause: error, |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +export function createImportReport(): ImportReport { |
| 109 | + return { |
| 110 | + errors: [], |
| 111 | + warnings: [], |
| 112 | + errorCountsByCode: {}, |
| 113 | + warningCountsByCode: {}, |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +export function addImportError(report: ImportReport, error: GtfsError): void { |
| 118 | + report.errors.push(error); |
| 119 | + report.errorCountsByCode[error.code] = |
| 120 | + (report.errorCountsByCode[error.code] ?? 0) + 1; |
| 121 | +} |
| 122 | + |
| 123 | +export function addImportWarning( |
| 124 | + report: ImportReport, |
| 125 | + warning: GtfsWarning, |
| 126 | +): void { |
| 127 | + report.warnings.push(warning); |
| 128 | + report.warningCountsByCode[warning.code] = |
| 129 | + (report.warningCountsByCode[warning.code] ?? 0) + 1; |
| 130 | +} |
| 131 | + |
| 132 | +export function formatGtfsError( |
| 133 | + error: unknown, |
| 134 | + options: { verbosity: 'user' | 'developer' } = { verbosity: 'developer' }, |
| 135 | +) { |
| 136 | + if (!isGtfsError(error)) { |
| 137 | + const message = error instanceof Error ? error.message : String(error); |
| 138 | + return options.verbosity === 'user' ? message : `UNKNOWN_ERROR: ${message}`; |
| 139 | + } |
| 140 | + |
| 141 | + if (options.verbosity === 'user') { |
| 142 | + return error.message; |
| 143 | + } |
| 144 | + |
| 145 | + return [ |
| 146 | + `${error.code}: ${error.message}`, |
| 147 | + `category=${error.category}`, |
| 148 | + error.statusCode !== undefined ? `statusCode=${error.statusCode}` : null, |
| 149 | + error.details ? `details=${JSON.stringify(error.details)}` : null, |
| 150 | + ] |
| 151 | + .filter(Boolean) |
| 152 | + .join(' | '); |
| 153 | +} |
0 commit comments