2023-02-11 09:05:50 +00:00
|
|
|
import type { StatusCode } from './utils/http-status.ts'
|
2023-01-12 09:53:13 +00: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 02:38:08 +00:00
|
|
|
super(options?.message)
|
2023-01-12 09:53:13 +00:00
|
|
|
this.res = options?.res
|
|
|
|
this.status = status
|
|
|
|
}
|
|
|
|
getResponse(): Response {
|
|
|
|
if (this.res) {
|
|
|
|
return this.res
|
|
|
|
}
|
|
|
|
return new Response(this.message, {
|
|
|
|
status: this.status,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|