marcushill / troubadour-server

1 stars 1 forks source link

Global Error Handler #13

Closed marcushill closed 7 years ago

jblairkiel commented 7 years ago

jblairkiel commented 7 years ago

For instance, invalid X-USER-ID returns: { "error": "User does not exist" }

marcushill commented 7 years ago

TLDR: Just throw an exception. I'll catch it.

I'm starting a global exception handler and introducing custom exceptions that should help with this. Ideally, if you have an error in a piece of the business logic, you should just throw an exception (possibly with a status code) and move on. We shouldn't have to track down all the route handlers to add duplicate try catch statements.

marcushill commented 7 years ago

Done. Now, you can get a properly formatted error from the API by throwing an Exception/Error. If you use the custom TroubadourError, you're also allowed to specify a status code. (Or you could just set status on a plain old Error object. Gotta love duck typing).