Any clean code for response exception handling? -_-
n
nschulzke
05/12/2022, 10:43 PM
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
Aleksei Tirman [JB]
05/13/2022, 7:52 AM
Could you please describe in what scenario do you want to handle exceptions?
e
enleur
05/13/2022, 8:03 AM
check out kotlin.Result
m
Muhammad Usman
05/13/2022, 10:21 AM
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."))
}
}