kotlinhttp4k

Idiomatic way of implementing global error handler in http4k


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?


Solution

  • 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/