Any clean code for response exception handling? -...
# ktor
h
Any clean code for response exception handling? -_-
n
If you're asking about ktor client, all the response exceptions extend
ResponseException
, so this code works fine if you don't need to distinguish between failure types:
Copy code
try {
    // Your request here
} catch (e: ResponseException) {
    // Do something
}
Is that what you mean?
a
Could you please describe in what scenario do you want to handle exceptions?
e
check out kotlin.Result
m
Ktor StatusPages Plugin might be helpful
Copy code
install(StatusPages) {
            exception<AuthenticationException> { cause ->
                call.respond(HttpStatusCode.Unauthorized)
            }
            exception<AuthorizationException> { cause ->
                call.respond(HttpStatusCode.Forbidden)
            }
            exception<ResponseException> { cause ->
                call.respond(mapOf("success" to false, "msg" to cause.message))
            }
            exception<Exception> { cause ->
                call.respond(mapOf("success" to false, "msg" to cause.message))
            }
            status(HttpStatusCode.Unauthorized) {
                call.respond(mapOf("success" to false, "msg" to "Your session expired. Please logout and login again."))
            }
        }
144 Views