I use retrofit for this request. You can see that the method returns a Response object, which is required to get some information about the progress of the request.
@POST("/test")
suspend fun sendTest(@Body model: TestModel): Response<Void>
I encountered a surprise in that specifying such a return leads to the fact that in the case of a backend response with the code != 200, the catch branches do not work. In general, it is clear why this is so - telling the retrofit that I want to get a Response from it, it removes responsibility for throwing exceptions. Question: maybe there is some annotation that will indicate that the retrofit should throw an exception with a Response.
I use retrofit for this request. You can see that the method returns a Response object, which is required to get some information about the progress of the request.
@POST("/test")
suspend fun sendTest(@Body model: TestModel): Response<Void>
I encountered a surprise in that specifying such a return leads to the fact that in the case of a backend response with the code != 200, the catch branches do not work. In general, it is clear why this is so - telling the retrofit that I want to get a Response from it, it removes responsibility for throwing exceptions. Question: maybe there is some annotation that will indicate that the retrofit should throw an exception with a Response.
You should try to catch more exceptions
suspend fun callSendTest(model: TestModel) {
try {
val response = sendTest(model)
if (response.isSuccessful) {
} else {
println("HTTP Error: ${response.code()}")
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: HttpException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
}
}