import type { Context } from '../context'; export namespace ErrorResponseUtil { /** * Ends the response of a failed request with `status` and `body` (and `contentType` when given). * A response the handler had already started is only ended; an ended one is left alone. */ export function end(ctx: Context, status: number, body: string, contentType?: string): void { const { response } = ctx; if (response.writableEnded) { return; } if (response.headersSent) { response.end(); return; } response.statusCode = status; if (contentType) { response.setHeader('Content-Type', contentType); } response.end(body); } }