Reputation: 2214
I have an API which sends me errors with a custom JSON under 500 error
so, here's my Api Interface:
@POST("${API_PREFIX}method")
fun callForIt(@Body request: MyRequest) : Single<MyResponse>
And here's how I call it:
api.callForIt(request)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
// Here I get perfect MyResponse object
},
{
// And here I get only throwable, but need to get info from the json
})
So, what I want is to describe a custom data class like
data class ErrorResponse(
val type: String,
val fatal: Boolean,
val msg: String
// etc
)
because the server is sending me valuable info in JSON and I need to obtain it, so is there any way to read it from onFailure()?
Upvotes: 0
Views: 93
Reputation: 9692
{ t: Throwable ->
if (t is HttpException) {
val errorMsg = t.response()!!.errorBody()!!.string()
val errorResponse = Gson().fromJson(errorMsg, ErrorResponse::class)
// handle the error with errorResponse...
}
else {
// handle the error with code >=500...
}
}
Upvotes: 1