I came across the same issue when writing my own REST services.
Let's start with the philosophy:
I like to think of my apps as a box. On the inside of the box are all the parts that I built and which have direct control. If something breaks down here, this is my mistake, it should crash, and I should read about it in the error log. At the edge of the window are all points of connection to the outside world - they should not be trusted. I avoid handling exceptions in the inner parts and use it as needed for the outer edge.
In similar projects, I worked on:
I usually have about a dozen user input checks. If something looks bad, I log it and return an error to the user. Having a stack trace does not really matter to me - if a user has forgotten a parameter, there is nothing in my code to find and fix. I would rather see a text log that says something like: "at 17:35, user X turned to path Y, but the Z parameter was missing."
I will organize my checks on functions that return ok or {error, string()} . The main function simply performs the check and returns ok if they are all passed, otherwise it returns the first error, which is then logged. Inside my validation functions, I use exception handling as needed, because I cannot consider all the ways that users can get corrupted.
As suggested by my colleagues, you can alternatively have each check to throw an exception instead of using a tuple.
As for your implementation, I think your idea of using a single exception handler is good if you have only one check. If you need more checks, you can implement something like the one described so that you can have a more specific log.
source share