@@ -25,6 +25,7 @@ import org.json.JSONException
2525import org.json.JSONObject
2626import timber.log.Timber
2727import java.io.IOException
28+ import java.net.HttpURLConnection
2829
2930class LocationSetter (appContext : Context , workerParams : WorkerParameters ) : CoroutineWorker(appContext, workerParams) {
3031 companion object {
@@ -78,9 +79,8 @@ class LocationSetter(appContext: Context, workerParams: WorkerParameters) : Coro
7879 override suspend fun doWork () = try {
7980 val lat = inputData.getDouble(KEY_LATITUDE , Double .NaN )
8081 val lon = inputData.getDouble(KEY_LONGITUDE , Double .NaN )
81- val time = inputData.getLong(KEY_TIME , 0 )
8282 val apiUrl = inputData.getString(KEY_API_URL )!!
83- val conn = ReactMapHttpEngine .connectWithCookie(apiUrl) { conn ->
83+ doWork(lat, lon, inputData.getLong( KEY_TIME , 0 ), apiUrl, ReactMapHttpEngine .connectWithCookie(apiUrl) { conn ->
8484 conn.doOutput = true
8585 conn.requestMethod = " POST"
8686 conn.addRequestProperty(" Content-Type" , " application/json" )
@@ -98,13 +98,38 @@ class LocationSetter(appContext: Context, workerParams: WorkerParameters) : Coro
9898 " human { current_profile_no name type } } }" )
9999 }.toString())
100100 }
101- }
102- when (val code = conn.responseCode) {
101+ })
102+ } catch (e: IOException ) {
103+ Timber .d(e)
104+ Result .retry()
105+ } catch (_: CancellationException ) {
106+ Result .failure()
107+ } catch (e: Exception ) {
108+ Timber .w(e)
109+ notifyError(e.readableMessage)
110+ Result .failure()
111+ }
112+ private fun notifyErrors (response : String , json : JSONObject ? = null) = notifyError(try {
113+ val errors = (json ? : JSONObject (response)).getJSONArray(" errors" )
114+ (0 until errors.length()).joinToString(" \n " ) { errors.getJSONObject(it).getString(" message" ) }
115+ } catch (e: JSONException ) {
116+ response
117+ })
118+ private suspend fun doWork (lat : Double , lon : Double , time : Long , apiUrl : String , conn : HttpURLConnection ): Result {
119+ return when (val code = conn.responseCode) {
103120 200 -> {
104121 val response = conn.inputStream.bufferedReader().readText()
105122 val human = try {
106- val webhook = JSONObject (response).getJSONObject(" data" ).getJSONObject(" webhook" )
107- if (webhook.opt(" human" ) == null ) {
123+ val obj = JSONObject (response)
124+ val webhook = obj.getJSONObject(" data" ).optJSONObject(" webhook" )
125+ if (webhook == null ) {
126+ notifyErrors(response, obj)
127+ if (obj.optJSONObject(" extensions" )?.optString(" code" ) == " INTERNAL_SERVER_ERROR" ) {
128+ Timber .w(response)
129+ } else Timber .w(Exception (response))
130+ return Result .retry()
131+ }
132+ if (webhook[" human" ] == JSONObject .NULL ) {
108133 withContext(Dispatchers .Main ) { BackgroundLocationReceiver .stop() }
109134 notifyError(app.getText(R .string.error_webhook_human_not_found))
110135 throw CancellationException ()
@@ -138,8 +163,7 @@ class LocationSetter(appContext: Context, workerParams: WorkerParameters) : Coro
138163 }
139164 else -> {
140165 val error = conn.findErrorStream.bufferedReader().readText()
141- val json = JSONObject (error).getJSONArray(" errors" )
142- notifyError((0 until json.length()).joinToString { json.getJSONObject(it).getString(" message" ) })
166+ notifyErrors(error)
143167 if (code == 401 || code == 511 ) {
144168 withContext(Dispatchers .Main ) { BackgroundLocationReceiver .stop() }
145169 Result .failure()
@@ -149,15 +173,6 @@ class LocationSetter(appContext: Context, workerParams: WorkerParameters) : Coro
149173 }
150174 }
151175 }
152- } catch (e: IOException ) {
153- Timber .d(e)
154- Result .retry()
155- } catch (_: CancellationException ) {
156- Result .failure()
157- } catch (e: Exception ) {
158- Timber .w(e)
159- notifyError(e.readableMessage)
160- Result .failure()
161176 }
162177
163178 override suspend fun getForegroundInfo () = ForegroundInfo (2 , Notification .Builder (app, CHANNEL_ID ).apply {
0 commit comments