2021-03-08 16:07:10 +00:00
|
|
|
import {ErrorResponseFactory} from "./ErrorResponseFactory";
|
|
|
|
import {HTTPError} from "../HTTPError";
|
|
|
|
import {HTTPStatus} from "@extollo/util"
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Helper that generates a new HTTPErrorResponseFactory given the HTTP status and message.
|
|
|
|
* @param status
|
|
|
|
* @param message
|
|
|
|
* @param output
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
export function http(status: HTTPStatus, message?: string, output: 'json' | 'html' | 'auto' = 'auto'): HTTPErrorResponseFactory {
|
|
|
|
return new HTTPErrorResponseFactory(new HTTPError(status, message), output)
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Response factory that renders the given HTTPError in the specified output format.
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
export class HTTPErrorResponseFactory extends ErrorResponseFactory {
|
|
|
|
constructor(
|
|
|
|
public readonly error: HTTPError,
|
|
|
|
output: 'json' | 'html' | 'auto' = 'auto', // FIXME xml support
|
|
|
|
) {
|
|
|
|
super(error, error.status, output)
|
|
|
|
}
|
|
|
|
}
|