2023-02-11 10:05:50 +01:00
|
|
|
import type { StatusCode } from './utils/http-status.ts'
|
2023-01-12 10:53:13 +01:00
|
|
|
|
|
|
|
type HTTPExceptionOptions = {
|
|
|
|
res?: Response
|
|
|
|
message?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export class HTTPException extends Error {
|
|
|
|
readonly res?: Response
|
|
|
|
readonly status: StatusCode
|
|
|
|
constructor(status: StatusCode = 500, options?: HTTPExceptionOptions) {
|
2023-04-26 04:38:08 +02:00
|
|
|
super(options?.message)
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|