3

I tried to find an answer in project's documentation but couldn't find anything useful.

What is the best practice to implement a global error handler that captures all the exceptions and returns appropriate http status based on the exception type?

alisabzevari
  • 8,008
  • 6
  • 43
  • 67

1 Answers1

4

For this you should implement an http4k Filter.

The API docs have a list of all the current filters that are implemented: https://www.http4k.org/api/org.http4k.filter/

Specifically for this, you want to add a ServerFilters.CatchAll into your filter chain. If you click through you can see the implementation:

https://www.http4k.org/api/org.http4k.filter/-server-filters/-catch-all/

David D
  • 239
  • 2
  • 4
  • Developers probably shouldn't use CatchAll out of the box, as it leaks stack traces into the response. You can now configure its error behaviour to log uncaught exceptions. – Duncan McGregor Apr 04 '22 at 08:10