Proper handling for kernel level errors

This commit is contained in:
Garrett Mills 2020-09-05 10:10:00 -05:00
parent f1301d24f7
commit c0e1e1d2a4
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

View File

@ -5,6 +5,7 @@ import {Collection} from '../../collection/Collection.ts'
import {Service} from '../../../../di/src/decorator/Service.ts'
import {Request} from '../Request.ts'
import {Logging} from '../../service/logging/Logging.ts'
import {error} from '../response/helpers.ts'
/**
* Interface for fluently registering kernel modules into the kernel.
@ -59,20 +60,26 @@ export default class Kernel extends AppClass {
public async handle(request: Request): Promise<Request> {
const logger = this.make(Logging)
for ( const module of this.preflight.toArray() ) {
try {
for (const module of this.preflight.toArray()) {
logger.verbose(`Applying pre-flight HTTP kernel module: ${module.constructor.name}`)
request = await module.apply(request)
}
if ( this.inflight ) {
if (this.inflight) {
logger.verbose(`Applying core HTTP kernel module: ${this.inflight.constructor.name}`)
request = await this.inflight.apply(request)
}
for ( const module of this.postflight.toArray() ) {
for (const module of this.postflight.toArray()) {
logger.verbose(`Applying post-flight HTTP kernel module: ${module.constructor.name}`)
request = await module.apply(request)
}
} catch (e: any) {
logger.error(e)
const error_response = error(e)
await error_response.write(request)
}
return request
}