внутри блока coroutineScope для вызова retotrfit (в последнем выпуске он поддерживает сопрограммы). Но когда удаленный URL-адрес неверен, исключение не было перехвачено, даже если оно включено в try / catch.
suspend fun fetchData() {
val service = ServiceFactory().createRetrofitService(RemoteDataServiice::class.java, baseUrl+"http:\\fake.url.com\", httpClient)
coroutineScope {
try {
val response = service.getRemoteData(path, headers, params)
try {
val headerList = response.headers()
val success = response.isSuccessful
val code = response.code() // http status code
var message = response.message() // HTTP status message
// ……
processResponse(response.body(), success, code, message, headerList)
} finally {
response.body()?.close()
}
} catch (t: Throwable) {
withContext(Dispatchers.Default) {
processResponse(null, false, -1, t.message, null)
}
}
}
//...... do something after the coroutineScope is complete
}
Исключение составляют только ошибки и вылеты. Как отловить исключение в coroutineScope {}?
другой вопрос, не думайте, что мне нужно поставить withContext(Dispatchers.Default)
в catch
случае, поскольку он все еще находится в coroutineScope{}
, верно?