HTTP errors!
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import ResponseFactory from './ResponseFactory.ts'
|
||||
import {Request} from '../Request.ts'
|
||||
import * as api from '../../support/api.ts'
|
||||
import {HTTPStatus} from '../../const/http.ts'
|
||||
|
||||
export default class ErrorResponseFactory extends ResponseFactory {
|
||||
protected target_mode: 'json' | 'html' = 'html'
|
||||
|
||||
constructor(
|
||||
public readonly error: Error,
|
||||
status: number = 500,
|
||||
status: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
output: 'json' | 'html' = 'html',
|
||||
) {
|
||||
super()
|
||||
|
||||
11
lib/src/http/response/HTTPErrorResponseFactory.ts
Normal file
11
lib/src/http/response/HTTPErrorResponseFactory.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import ErrorResponseFactory from './ErrorResponseFactory.ts'
|
||||
import HTTPError from '../../error/HTTPError.ts'
|
||||
|
||||
export default class HTTPErrorResponseFactory extends ErrorResponseFactory {
|
||||
constructor(
|
||||
public readonly error: HTTPError,
|
||||
output: 'json' | 'html',
|
||||
) {
|
||||
super(error, error.http_status, output)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,16 @@
|
||||
import AppClass from '../../lifecycle/AppClass.ts'
|
||||
import {Request} from '../Request.ts'
|
||||
import {HTTPStatus} from '../../const/http.ts'
|
||||
|
||||
export default abstract class ResponseFactory extends AppClass {
|
||||
protected target_status: number = 200
|
||||
protected target_status: HTTPStatus = HTTPStatus.OK
|
||||
|
||||
public async write(request: Request): Promise<Request> {
|
||||
request.response.status = this.target_status
|
||||
return request
|
||||
}
|
||||
|
||||
public status(status: number): ResponseFactory {
|
||||
public status(status: HTTPStatus): ResponseFactory {
|
||||
this.target_status = status
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import ResponseFactory from './ResponseFactory.ts'
|
||||
import {Request} from '../Request.ts'
|
||||
import {HTTPStatus} from '../../const/http.ts'
|
||||
|
||||
export default class TemporaryRedirectResponseFactory extends ResponseFactory {
|
||||
protected target_status: number = 302
|
||||
protected target_status: HTTPStatus = HTTPStatus.TEMPORARY_REDIRECT
|
||||
|
||||
constructor(
|
||||
public readonly destination: string,
|
||||
|
||||
@@ -5,6 +5,9 @@ import ErrorResponseFactory from './ErrorResponseFactory.ts'
|
||||
import {Rehydratable} from '../../support/Rehydratable.ts'
|
||||
import DehydratedStateResponseFactory from './DehydratedStateResponseFactory.ts'
|
||||
import TemporaryRedirectResponseFactory from './TemporaryRedirectResponseFactory.ts'
|
||||
import {HTTPStatus} from "../../const/http.ts";
|
||||
import HTTPErrorResponseFactory from "./HTTPErrorResponseFactory.ts";
|
||||
import HTTPError from "../../error/HTTPError.ts";
|
||||
|
||||
export function json(value: any): JSONResponseFactory {
|
||||
return make(JSONResponseFactory, value)
|
||||
@@ -26,3 +29,7 @@ export function dehydrate(value: Rehydratable): DehydratedStateResponseFactory {
|
||||
export function redirect(destination: string): TemporaryRedirectResponseFactory {
|
||||
return make(TemporaryRedirectResponseFactory, destination)
|
||||
}
|
||||
|
||||
export function http(status: HTTPStatus, message?: string) {
|
||||
return make(HTTPErrorResponseFactory, new HTTPError(status, message))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user