I was wondering what folks think would be a good way of handling errors in a REST-style api written in Clojure, using the Ring library.
One approach taken by Paul Umbers in his Clojure RESTful API tutorial is to let exceptions happen naturally and allow them to bubble all the way up to a piece of middleware specialized in turning exceptions to specific HTTP status codes.
Basically, DB constraints will throw their own specific errors (e.g PSQLException), model validators will throw another type, all under the code 400 umbrella. Unknown exceptions will be caught by the generic Exception handler and return the 500 code.
A few thoughts:
- Can we do better? Is this the wrong design for some specific reason?
- Many will claim that handling the generic Exception type is bad practice. Can such an argument be made here as well?
Thanks!