0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 11:17:33 +01:00
hono/deno_dist/http-exception.ts
2023-02-11 18:05:50 +09:00

27 lines
667 B
TypeScript

import type { StatusCode } from './utils/http-status.ts'
import { getStatusText } from './utils/http-status.ts'
type HTTPExceptionOptions = {
res?: Response
message?: string
}
export class HTTPException extends Error {
readonly res?: Response
readonly status: StatusCode
constructor(status: StatusCode = 500, options?: HTTPExceptionOptions) {
super(options?.message || getStatusText(status))
this.res = options?.res
this.status = status
}
getResponse(): Response {
if (this.res) {
return this.res
}
return new Response(this.message, {
status: this.status,
statusText: getStatusText(this.status),
})
}
}