https://kotlinlang.org logo
#http4k
Title
# http4k
d

dave

03/28/2019, 5:21 AM
@krtko for that you should use a Filter and just wrap your handler (or entire app) using
then()
. We provide the one below which should give you a general idea, but you can easily write your own to plug in whatever logger you need:
Copy code
object CatchAll {
        operator fun invoke(errorStatus: Status = INTERNAL_SERVER_ERROR): Filter = Filter { next ->
            {
                try {
                    next(it)
                } catch (e: Exception) {
                    val sw = StringWriter()
                    e.printStackTrace(PrintWriter(sw))
                    Response(errorStatus).body(sw.toString())
                }
            }
        }
    }
k

krtko

03/28/2019, 4:38 PM
Thanks so if I wanted to add some general headers onto all of my responses, using a filter would be the route to go?
d

dave

03/28/2019, 4:39 PM
yep
k

krtko

03/28/2019, 4:40 PM
Thank you
8 Views