lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.54k stars 499 forks source link

[Koa] global error handler not called #218

Closed RafaelKr closed 5 years ago

RafaelKr commented 6 years ago

Hi,

I want to integrate bugsnag into my project. For that you have to implement the global Koa error handler: app.on('error', bugsnag.koaHandler) but the handler is never called.

https://github.com/koajs/koa/wiki/Error-Handling

Is there any other way to implement that?

For my local project I found a workaround: https://github.com/RafaelKr/tsoa/commit/5cdb2768db374525466d085032e1276951a40023 But now one of the Koa tests outputs an error:

{ BadRequestError: error thrown
    at Object.throw (/home/rafael/coding/Office/Scatch/code/server/tsoa/node_modules/koa/lib/context.js:93:11)
    at /home/rafael/coding/Office/Scatch/code/server/tsoa/tests/fixtures/koa/routes.ts:2021:22
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7) message: 'error thrown' }
    ✓ returns error if thrown in controller

I tried to fix it with app.silent = true or app.on('error', () => {}) in the file / tests/fixtures/koa/server.ts, but the error message is appearing nevertheless and the error handler is never called.

Edit: I think the error output stems from /tests/fixtures/koa/server.ts#L33, somehow I overread that while testing.

In my local app the error handler is called when I do the following:

import { Controller, Get, Route } from 'tsoa'

@Route('error')
export class ErrorTestController extends Controller {

  @Get()
  public async getError (): Promise<any> {
    throw {
      status: 500,
      message: 'test'
    }
  }

}
lukeautry commented 5 years ago

@RafaelKr I think this is fixed as of 2.3.7. Let me know if it's still happening.