2023-01-12 10:53:13 +01:00
|
|
|
import type { StatusCode } from './http-status.ts'
|
|
|
|
import { getStatusText } from './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) {
|
2023-01-22 07:06:55 +01:00
|
|
|
super(options?.message || getStatusText(status))
|
2023-01-12 10:53:13 +01:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|