Passing errors to the Clojure Ring REST-like API?

I was wondering what people find to be a good way to handle REST style api errors written in Clojure using the Ring library.

One approach taken by Paul Umbers in his Clojure RESTful API tutorial is to eliminate natural exceptions and allow them to bubble completely down to the middleware part specializing in exception handling for specific HTTP status codes.

Basically, DB constraints will throw their own specific errors (for example, PSQLException), model validators will throw a different type, all under code umbrella 400. Unknown exceptions will be caught by the general Exception handler and return code 500.

A few thoughts:

  • Can we do better? Is this a wrong design for a reason?
  • Many argue that handling a typical type of exception is bad practice. Could such an argument be here and here?

Thanks!

+4
source share
1 answer

I am by no means an expert in this particular field, but since no one else has weighed, I will give my two cents.

The solution you're connected with seems like a reasonable approach to me. Given the little cooperation between your handlers and the middleware exceptions, you can also flag your exceptions with additional information that may be useful in visualizing the error response, without actual error handling information crawling into your application logic.

So, to your first question: perhaps you could adapt your system to a specific specific use case, but as a general-purpose error handling scheme, this seems pretty good. There is nothing that pops up on me as a direct "wrong."

To your second question: it’s bad practice to catch the general type of Exception when you know that you are looking for a more specific one because you want to avoid combining expected and unexpected errors. If you know that there is a MissingResourceException possibility when you search for packages, you would not want your exception handler to accidentally remove NullPointerException bubbles from the actual error in your code.

In this case, however, I would say that looking for a typical type of Exception is exactly what you need to do. Instead of handling certain conditions, such as MissingResourceException , the goal of this top-level handler is to capture everything that your application logic does not and convert it to error information that matters to your API client. This is kind of the last line of defense between your implementation and its consumer at the other end.

+1
source

Source: https://habr.com/ru/post/1479276/


All Articles