A.Rubinstein
A.Rubinstein

Reputation: 133

How to handle error response with retrofit + coroutines

I'm new with coroutines. Trying to use retrofit + coroutines + Jake Wharton's CoroutineCallAdapterFactory by this tutorial

But don't get how to handle json response errors. For example error could be like that:

{
  "code": 105,
  "error": "invalid field name: bl!ng"
} 

I think adding code and error fields (and checking object for null fields) in TmdbMovie class - it's wrong. And then - how to use error fields after launching coroutine in TmdbViewModel ?

Upvotes: 7

Views: 5967

Answers (1)

musooff
musooff

Reputation: 6852

try/catch the await as recommended by @JakeWharton in this issue

try {
    youService().await()
} catch(ex: HttpException) {
    // do your handling here
}

Upvotes: 8

Related Questions