• Add a method of a controller as an error handler.

    Returns MethodDecorator

    The new decorator function.

    Example

    import { Controller, ControllerBase, ErrorHandler, GET, IHttpRequest, IHttpResponse } from '@egomobile/http-server'

    @Controller()
    export default class MyController extends ControllerBase {
    @GET()
    async index(request: IHttpRequest, response: IHttpResponse) {
    throw new Error('Something went wrong!')
    }

    @ErrorHandler() // mark that method as default error handler
    // inside that controller
    async handleError(error: any, request: IHttpRequest, response: IHttpResponse) {
    const errorMessage = Buffer.from('ERROR: ' + String(error), 'utf8')

    response.writeHead(500, {
    'Content-Length': String(errorMessage.length)
    })
    response.write(errorMessage)

    response.end()
    }
    }

Generated using TypeDoc