Skip to content

Errors thrown by express middleware on JSONController routes are not caught properly #563

@prodigga

Description

@prodigga

Hi there

I noticed that if I throw an error inside of a route, it is caught and the client receives a JSON body response. But if I throw an error inside of some middleware, UseBefore the route, then the error is returned as the body of the response.

For example, this is the body of the response from a custom middleware (AuthrizePlayer) that throws an error:

Error
at AuthorizePlayer.use (C:\Users\Tim\Desktop\Heroku\cargame2020\dist\Middleware\AuthorizePlayer.js:6:15)
at C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\routing-controllers\driver\express\ExpressDriver.js:381:86
at Layer.handle [as handle_request]
(C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request]
(C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:275:10)
at expressInit (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request]
(C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\router\index.js:275:10)
at query (C:\Users\Tim\Desktop\Heroku\cargame2020\node_modules\express\lib\middleware\query.js:45:5)

Compared to the same error (new Error()) being throw inside of the route itself:

{
    "name": "Error",
    "stack": "Error\n    at PlayerController.GetMyName (C:\\Users\\Tim\\Desktop\\Heroku\\cargame2020\\dist\\Controllers\\PlayerController.js:40:19)\n    at ActionMetadata.callMethod (C:\\Users\\Tim\\Desktop\\Heroku\\cargame2020\\node_modules\\routing-controllers\\metadata\\ActionMetadata.js:108:48)\n    at C:\\Users\\Tim\\Desktop\\Heroku\\cargame2020\\node_modules\\routing-controllers\\RoutingControllers.js:99:140"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: awaiting answerAwaiting answer from the author of issue or PR.type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions