|
| 1 | +import { |
| 2 | + createErrorHandler, |
| 3 | + HTTP_ERRORS, |
| 4 | + renderProductionErrorPage, |
| 5 | +} from 'ts-error-handling' |
| 6 | +import type { |
| 7 | + ErrorPageConfig, |
| 8 | + HttpStatusCode, |
| 9 | + RequestContext, |
| 10 | + RoutingContext, |
| 11 | +} from 'ts-error-handling' |
| 12 | + |
1 | 13 | export class HttpError extends Error { |
2 | 14 | constructor(public status: number, message: string) { |
3 | 15 | super(message) |
4 | 16 | this.name = 'Server Error!' |
5 | 17 | } |
6 | 18 | } |
| 19 | + |
| 20 | +/** |
| 21 | + * HTTP error handler with Ignition-style error pages |
| 22 | + */ |
| 23 | +export class HttpErrorHandler { |
| 24 | + private handler = createErrorHandler() |
| 25 | + private isDevelopment: boolean |
| 26 | + |
| 27 | + constructor(options?: { |
| 28 | + isDevelopment?: boolean |
| 29 | + config?: ErrorPageConfig |
| 30 | + }) { |
| 31 | + this.isDevelopment = options?.isDevelopment ?? process.env.NODE_ENV !== 'production' |
| 32 | + |
| 33 | + if (options?.config) { |
| 34 | + this.handler = createErrorHandler(options.config) |
| 35 | + } |
| 36 | + |
| 37 | + // Set framework info |
| 38 | + this.handler.setFramework('Stacks') |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Set request context |
| 43 | + */ |
| 44 | + setRequest(request: Request | RequestContext): this { |
| 45 | + this.handler.setRequest(request) |
| 46 | + return this |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Set routing context |
| 51 | + */ |
| 52 | + setRouting(routing: RoutingContext): this { |
| 53 | + this.handler.setRouting(routing) |
| 54 | + return this |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Add a query to track |
| 59 | + */ |
| 60 | + addQuery(query: string, time?: number, connection?: string): this { |
| 61 | + this.handler.addQuery(query, time, connection) |
| 62 | + return this |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Handle an error and return an HTML response |
| 67 | + */ |
| 68 | + handle(error: Error, status: number = 500): Response { |
| 69 | + if (this.isDevelopment) { |
| 70 | + return this.handler.handleError(error, status) |
| 71 | + } |
| 72 | + |
| 73 | + // Production: show simple error page without details |
| 74 | + return new Response(renderProductionErrorPage(status), { |
| 75 | + status, |
| 76 | + headers: { 'Content-Type': 'text/html; charset=utf-8' }, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Create a 404 Not Found response |
| 82 | + */ |
| 83 | + notFound(message?: string): Response { |
| 84 | + const error = new HttpError(404, message || 'The requested resource could not be found.') |
| 85 | + return this.handle(error, 404) |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Create a 500 Internal Server Error response |
| 90 | + */ |
| 91 | + serverError(error: Error): Response { |
| 92 | + return this.handle(error, 500) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Create a 403 Forbidden response |
| 97 | + */ |
| 98 | + forbidden(message?: string): Response { |
| 99 | + const error = new HttpError(403, message || 'You do not have permission to access this resource.') |
| 100 | + return this.handle(error, 403) |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Create a 401 Unauthorized response |
| 105 | + */ |
| 106 | + unauthorized(message?: string): Response { |
| 107 | + const error = new HttpError(401, message || 'Authentication is required to access this resource.') |
| 108 | + return this.handle(error, 401) |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Create a 400 Bad Request response |
| 113 | + */ |
| 114 | + badRequest(message?: string): Response { |
| 115 | + const error = new HttpError(400, message || 'The request was malformed or invalid.') |
| 116 | + return this.handle(error, 400) |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Create a 422 Unprocessable Entity response |
| 121 | + */ |
| 122 | + validationError(message?: string): Response { |
| 123 | + const error = new HttpError(422, message || 'The request was well-formed but could not be processed.') |
| 124 | + return this.handle(error, 422) |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Create a 429 Too Many Requests response |
| 129 | + */ |
| 130 | + tooManyRequests(message?: string): Response { |
| 131 | + const error = new HttpError(429, message || 'You have exceeded the rate limit.') |
| 132 | + return this.handle(error, 429) |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Create a 503 Service Unavailable response |
| 137 | + */ |
| 138 | + serviceUnavailable(message?: string): Response { |
| 139 | + const error = new HttpError(503, message || 'The service is temporarily unavailable.') |
| 140 | + return this.handle(error, 503) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Create a new HTTP error handler |
| 146 | + */ |
| 147 | +export function createHttpErrorHandler(options?: { |
| 148 | + isDevelopment?: boolean |
| 149 | + config?: ErrorPageConfig |
| 150 | +}): HttpErrorHandler { |
| 151 | + return new HttpErrorHandler(options) |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Quick helper to render an error page for HTTP errors |
| 156 | + */ |
| 157 | +export function renderHttpError( |
| 158 | + error: Error, |
| 159 | + request?: Request, |
| 160 | + options?: { |
| 161 | + status?: number |
| 162 | + isDevelopment?: boolean |
| 163 | + config?: ErrorPageConfig |
| 164 | + }, |
| 165 | +): Response { |
| 166 | + const handler = createHttpErrorHandler({ |
| 167 | + isDevelopment: options?.isDevelopment, |
| 168 | + config: options?.config, |
| 169 | + }) |
| 170 | + |
| 171 | + if (request) { |
| 172 | + handler.setRequest(request) |
| 173 | + } |
| 174 | + |
| 175 | + return handler.handle(error, options?.status) |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * Express/Hono style error middleware |
| 180 | + */ |
| 181 | +export function errorMiddleware(options?: { |
| 182 | + isDevelopment?: boolean |
| 183 | + config?: ErrorPageConfig |
| 184 | +}) { |
| 185 | + const handler = createHttpErrorHandler(options) |
| 186 | + |
| 187 | + return async (error: Error, request: Request): Promise<Response> => { |
| 188 | + handler.setRequest(request) |
| 189 | + |
| 190 | + // Determine status code |
| 191 | + let status = 500 |
| 192 | + if (error instanceof HttpError) { |
| 193 | + status = error.status |
| 194 | + } |
| 195 | + else if ('status' in error && typeof error.status === 'number') { |
| 196 | + status = error.status as number |
| 197 | + } |
| 198 | + else if ('statusCode' in error && typeof error.statusCode === 'number') { |
| 199 | + status = error.statusCode as number |
| 200 | + } |
| 201 | + |
| 202 | + return handler.handle(error, status) |
| 203 | + } |
| 204 | +} |
0 commit comments