Skip to content

Commit 07e7a9b

Browse files
committed
Fix error parsing
1 parent eef6a15 commit 07e7a9b

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

app/src/main/java/be/mygod/reactmap/follower/LocationSetter.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,22 @@ class LocationSetter(appContext: Context, workerParams: WorkerParameters) : Coro
106106
notifyError(e.readableMessage)
107107
Result.failure()
108108
}
109-
private fun notifyErrors(response: String, json: JSONObject? = null) = notifyError(try {
110-
val errors = (json ?: JSONObject(response)).getJSONArray("errors")
111-
(0 until errors.length()).joinToString("\n") { errors.getJSONObject(it).getString("message") }
112-
} catch (e: JSONException) {
113-
response
114-
})
109+
private fun notifyErrors(response: String, json: JSONObject? = null): Boolean {
110+
var shouldWarn = true
111+
notifyError(try {
112+
val errors = (json ?: JSONObject(response)).getJSONArray("errors")
113+
(0 until errors.length()).joinToString("\n") {
114+
val error = errors.getJSONObject(it)
115+
if (error.optJSONObject("extensions")?.optString("code") == "INTERNAL_SERVER_ERROR") {
116+
shouldWarn = false
117+
}
118+
error.getString("message")
119+
}
120+
} catch (e: JSONException) {
121+
response
122+
})
123+
return shouldWarn
124+
}
115125
private suspend fun doWork(lat: Double, lon: Double, time: Long, apiUrl: String, conn: HttpURLConnection): Result {
116126
return when (val code = conn.responseCode) {
117127
200 -> {
@@ -120,10 +130,7 @@ class LocationSetter(appContext: Context, workerParams: WorkerParameters) : Coro
120130
val obj = JSONObject(response)
121131
val webhook = obj.getJSONObject("data").optJSONObject("webhook")
122132
if (webhook == null) {
123-
notifyErrors(response, obj)
124-
if (obj.optJSONObject("extensions")?.optString("code") == "INTERNAL_SERVER_ERROR") {
125-
Timber.w(response)
126-
} else Timber.w(Exception(response))
133+
if (notifyErrors(response, obj)) Timber.w(response) else Timber.w(Exception(response))
127134
return Result.retry()
128135
}
129136
if (webhook["human"] == JSONObject.NULL) {

0 commit comments

Comments
 (0)