Here is code from Auth guard:
val sessionToken = call.request.cookies.get("session_token")
if (sessionToken == null) throw HttpException(HttpStatusCode.Unauthorized, "Unauthorized", "User is not authorized, please login")
HttpException:
class HttpException (
val statusCode: HttpStatusCode,
val errorType: String,
val errorDetail: String,
) : Exception("HttpException")
In App.kt :
install(ContentNegotiation) { json() }
.....
@Serializable
data class ErrResponseBody (val errorType: String, val errorDetail: String)
install(StatusPages) {
exception<Throwable> { call, cause ->
when(cause) {
// http error
is HttpException -> {
val responseBody = ErrResponseBody(cause.errorType, cause.errorDetail)
println(responseBody)
//call.respond(cause.statusCode, "AAAAAAAAAAAAAAA") // this one work for some reason!
call.respond(cause.statusCode, responseBody)
}
// 500
else -> {
val responseBody = ErrResponseBody("Internal server error", "")
call.respond(HttpStatusCode.InternalServerError, responseBody)
}
}
}
}
I’ve made sure execution happens till error handler
So, here is the problem:
Serialization there not works, and call.response(T)
not works
With returning ErrResponseBody
(serialization required)
enter image description here
Without (with “AAAAAAAAA” instead of T)
enter image description here