HTTP errors for not found, not implemented, method not allowed

This commit is contained in:
garrettmills 2020-07-30 09:39:05 -05:00
parent f1c4fbdb95
commit 48ead77765
No known key found for this signature in database
GPG Key ID: 6ACD58D6ADACFC6E
3 changed files with 21 additions and 15 deletions

View File

@ -24,6 +24,4 @@ documentation & docs site
events and observables? events and observables?
no matching route handler no matching route handler
request timeout request timeout
not handlers -> not implemented
method not allowed
error response handler figure out json error response handler figure out json

View File

@ -4,7 +4,8 @@ import Kernel from '../Kernel.ts'
import {Logging} from '../../../service/logging/Logging.ts' import {Logging} from '../../../service/logging/Logging.ts'
import {Request} from '../../Request.ts' import {Request} from '../../Request.ts'
import ResponseFactory from '../../response/ResponseFactory.ts' import ResponseFactory from '../../response/ResponseFactory.ts'
import ErrorResponseFactory from "../../response/ErrorResponseFactory.ts"; import {http, error} from '../../response/helpers.ts'
import {HTTPStatus} from '../../../const/http.ts'
@Injectable() @Injectable()
export default class ApplyRouteHandlers extends Module { export default class ApplyRouteHandlers extends Module {
@ -19,12 +20,19 @@ export default class ApplyRouteHandlers extends Module {
} }
public async apply(request: Request): Promise<Request> { public async apply(request: Request): Promise<Request> {
if ( if ( !request.route ) { // Route not found
!request.route const factory = http(HTTPStatus.NOT_FOUND)
|| !request.route.handlers return await factory.write(request)
|| request.route.handlers.length < 1 }
) {
return request if ( !Array.isArray(request.route.handlers) ) { // Route found but invalid HTTP method
const factory = http(HTTPStatus.METHOD_NOT_ALLOWED)
return await factory.write(request)
}
if ( request.route.handlers.length < 1 ) { // Route and method found, but no handlers
const factory = http(HTTPStatus.NOT_IMPLEMENTED)
return await factory.write(request)
} }
let current_request: Request = request let current_request: Request = request
@ -43,8 +51,8 @@ export default class ApplyRouteHandlers extends Module {
this.logger.error(e) this.logger.error(e)
// TODO determine response type (html | json, &c.) // TODO determine response type (html | json, &c.)
const error_response: ErrorResponseFactory = this.make(ErrorResponseFactory, e) const factory = error(e)
return await error_response.write(request) return await factory.write(request)
} }
} }

View File

@ -5,9 +5,9 @@ import ErrorResponseFactory from './ErrorResponseFactory.ts'
import {Rehydratable} from '../../support/Rehydratable.ts' import {Rehydratable} from '../../support/Rehydratable.ts'
import DehydratedStateResponseFactory from './DehydratedStateResponseFactory.ts' import DehydratedStateResponseFactory from './DehydratedStateResponseFactory.ts'
import TemporaryRedirectResponseFactory from './TemporaryRedirectResponseFactory.ts' import TemporaryRedirectResponseFactory from './TemporaryRedirectResponseFactory.ts'
import {HTTPStatus} from "../../const/http.ts"; import {HTTPStatus} from '../../const/http.ts'
import HTTPErrorResponseFactory from "./HTTPErrorResponseFactory.ts"; import HTTPErrorResponseFactory from './HTTPErrorResponseFactory.ts'
import HTTPError from "../../error/HTTPError.ts"; import HTTPError from '../../error/HTTPError.ts'
export function json(value: any): JSONResponseFactory { export function json(value: any): JSONResponseFactory {
return make(JSONResponseFactory, value) return make(JSONResponseFactory, value)
@ -30,6 +30,6 @@ export function redirect(destination: string): TemporaryRedirectResponseFactory
return make(TemporaryRedirectResponseFactory, destination) return make(TemporaryRedirectResponseFactory, destination)
} }
export function http(status: HTTPStatus, message?: string) { export function http(status: HTTPStatus, message?: string): HTTPErrorResponseFactory {
return make(HTTPErrorResponseFactory, new HTTPError(status, message)) return make(HTTPErrorResponseFactory, new HTTPError(status, message))
} }