Latest

Thursday, July 6, 2017

API - Returning Domain 'Errors' vs throwing Exceptions

Asked by: djt


I have a Web API (Laravel / PHP), that essentially handles two types of errors:

1) Domain Exceptions - These are exceptions that we write into our Application, and throw when a user does something wrong (ie. "that username is already taken!")

2) Actual Exceptions - These would be exceptions that are actually a cause for concern. Like "Could not connect to database"

In both cases, an Exception is thrown with a status code that is non-200, with the appropriate message / details. In the case of domain-specific errors, the user gets JSON back with the appropriate message. In the case of something like a "database connection" error, the user gets a generic error message, and we get the report with the stack trace.

For the first case, is it accurate to be throwing an actual Exception?

I was thinking about this because our application is hooked up to New Relic, and sometimes we get these alerts saying that the error threshold has been exceeded, when in fact, it generally means someone forgot their username and typed the wrong one in about 10 times in a row...

Is there a better way of handling these non-critical application errors?



Source

No comments:

Post a Comment

Adbox