-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApiCallsHandler.kt
69 lines (59 loc) · 2.15 KB
/
ApiCallsHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.latienda.core
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.HttpException
import java.io.IOException
import java.net.SocketTimeoutException
//Video: https://youtu.be/rWbv2zU4Zqw
object ApiCallsHandler {
private const val MESSAGE_KEY = "message"
private const val ERROR_KEY = "error"
enum class ErrorType {
NETWORK, // IO
TIMEOUT, // Socket
UNKNOWN //Anything else
}
public fun getErrorMessage(responseBody: ResponseBody?): String {
return try {
val jsonObject = JSONObject(responseBody!!.string())
when {
jsonObject.has(MESSAGE_KEY) -> jsonObject.getString(MESSAGE_KEY)
jsonObject.has(ERROR_KEY) -> jsonObject.getString(ERROR_KEY)
else -> "Something wrong happened"
}
} catch (e: Exception) {
"Something wrong happened"
}
}
interface RemoteErrorEmitter {
fun onError(msg: String)
fun onError(errorType: ErrorType)
}
public suspend inline fun <T> safeApiCall(
emitter: RemoteErrorEmitter,
crossinline responseFunction: suspend () -> T,
): T? {
return try {
val response = withContext(Dispatchers.IO) { responseFunction.invoke() }
response
} catch (e: Exception) {
withContext(Dispatchers.Main) {
e.printStackTrace()
Log.e("ApiCalls", "Call error: ${e.localizedMessage}", e.cause)
when (e) {
is HttpException -> {
val body = e.response()?.errorBody()
emitter.onError(getErrorMessage(body))
}
is SocketTimeoutException -> emitter.onError(ErrorType.TIMEOUT)
is IOException -> emitter.onError(ErrorType.NETWORK)
else -> emitter.onError(ErrorType.UNKNOWN)
}
}
null
}
}
}