|
| 1 | +import { Request, Response } from "cross-fetch"; |
| 2 | +import Negotiator from "negotiator"; |
| 3 | +import escapeHTML from "escape-html"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Used to properly space HTML output. |
| 7 | + */ |
| 8 | +const DOUBLE_SPACE_REGEXP = /\x20{2}/g; |
| 9 | + |
| 10 | +/** |
| 11 | + * Boom-compatible output. |
| 12 | + */ |
| 13 | +interface Output { |
| 14 | + status: number; |
| 15 | + headers: Record<string, string>; |
| 16 | + payload: object; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Convert an error into an "output" object. |
| 21 | + */ |
| 22 | +function toOutput(err: any, production: boolean): Output { |
| 23 | + const error: { |
| 24 | + output?: { |
| 25 | + statusCode?: number; |
| 26 | + headers?: Record<string, string>; |
| 27 | + payload?: any; |
| 28 | + }; |
| 29 | + statusCode?: number; |
| 30 | + status?: number; |
| 31 | + headers?: Record<string, string>; |
| 32 | + message?: string; |
| 33 | + } = |
| 34 | + err == null |
| 35 | + ? { message: `Empty error: ${err}` } |
| 36 | + : typeof err === "object" |
| 37 | + ? err |
| 38 | + : { message: String(err) }; |
| 39 | + |
| 40 | + const output = error.output || {}; |
| 41 | + const status = |
| 42 | + Number(output.statusCode || error.statusCode || error.status) || 500; |
| 43 | + const headers = output.headers || error.headers || {}; |
| 44 | + const payload = output.payload || { |
| 45 | + status, |
| 46 | + error: `${status} Error`, |
| 47 | + message: (production ? undefined : error.message) || "Error", |
| 48 | + }; |
| 49 | + |
| 50 | + return { status, headers, payload }; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Render HTML response. |
| 55 | + */ |
| 56 | +function renderHTML(req: Request, output: Output) { |
| 57 | + const content = escapeHTML(JSON.stringify(output.payload, null, 2)); |
| 58 | + |
| 59 | + return new Response( |
| 60 | + `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Error</title></head><body><pre>${content.replace( |
| 61 | + DOUBLE_SPACE_REGEXP, |
| 62 | + " " |
| 63 | + )}</pre></body></html>`, |
| 64 | + { |
| 65 | + status: output.status, |
| 66 | + headers: { |
| 67 | + "Content-Type": "text/html", |
| 68 | + "X-Content-Type-Options": "nosniff", |
| 69 | + "Content-Security-Policy": "default-src 'self'", |
| 70 | + ...output.headers, |
| 71 | + }, |
| 72 | + } |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Send JSON response. |
| 78 | + */ |
| 79 | +function renderJSON(req: Request, output: Output) { |
| 80 | + return new Response(JSON.stringify(output.payload), { |
| 81 | + status: output.status, |
| 82 | + headers: { |
| 83 | + "Content-Type": "application/json", |
| 84 | + "X-Content-Type-Options": "nosniff", |
| 85 | + ...output.headers, |
| 86 | + }, |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Render HTTP response. |
| 92 | + */ |
| 93 | +function render(req: Request, output: Output) { |
| 94 | + const negotiator = new Negotiator({ |
| 95 | + headers: { |
| 96 | + accept: req.headers.get("accept") || undefined, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + const type = negotiator.mediaType(["text/html", "application/json"]); |
| 101 | + if (type === "text/html") return renderHTML(req, output); |
| 102 | + return renderJSON(req, output); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Error handler options. |
| 107 | + */ |
| 108 | +export interface Options { |
| 109 | + production?: boolean; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Render errors into a response object. |
| 114 | + */ |
| 115 | +export function errorHandler( |
| 116 | + req: Request, |
| 117 | + options: Options = {} |
| 118 | +): (err: any) => Response { |
| 119 | + const production = options.production !== false; |
| 120 | + |
| 121 | + return function errorMiddleware(err: unknown) { |
| 122 | + return render(req, toOutput(err, production)); |
| 123 | + }; |
| 124 | +} |
0 commit comments