From 1b53c35ec54143cd953a8eb567db376e41f9e053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 7 Oct 2019 20:50:44 +0200 Subject: [PATCH] [APIv2] Fix Librus grades exception. Improve error handling and sync cancellation. --- .../edziennik/api/v2/ApiService.kt | 1 + .../edziennik/api/v2/librus/Librus.kt | 3 +-- .../edziennik/api/v2/librus/data/LibrusApi.kt | 3 ++- .../edziennik/api/v2/librus/data/LibrusData.kt | 4 ++++ .../api/v2/librus/data/api/LibrusApiGrades.kt | 2 +- .../edziennik/api/v2/models/ApiError.kt | 2 +- .../edziennik/api/v2/models/Data.kt | 17 +++++++++++++++-- 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt index 280b5c64..f76a792b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt @@ -84,6 +84,7 @@ class ApiService : Service() { apiError.profileId = taskProfileId EventBus.getDefault().post(SyncErrorEvent(apiError)) errorList.add(apiError) + apiError.throwable?.printStackTrace() if (apiError.isCritical) { notification.setCriticalError().post() taskRunning = false diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt index 66594fd7..d8b9e596 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt @@ -133,8 +133,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va } override fun cancel() { - d(TAG, "Cancelled") - cancelled = true + data.cancel() } private fun wrapCallback(callback: EdziennikCallback): EdziennikCallback { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt index 497d4845..89ce94a7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt @@ -77,7 +77,8 @@ open class LibrusApi(open val data: DataLibrus) { data.error(ApiError(tag, EXCEPTION_LIBRUS_API_REQUEST) .withResponse(response) .withThrowable(e) - .withApiResponse(json)) + .withApiResponse(json) + .setCritical(false)) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index fc91c2a2..84271b60 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -41,6 +41,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) { Utils.d(TAG, "Using endpoint $endpointId") + if (data.cancelled) { + Utils.d(TAG, "Skip endpoint $endpointId; cancelled") + return + } when (endpointId) { ENDPOINT_LIBRUS_API_ME -> { data.startProgress(R.string.edziennik_progress_endpoint_student_info) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt index c6ae0c4a..1d3de92b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt @@ -25,7 +25,7 @@ class LibrusApiGrades(override val data: DataLibrus, grades?.forEach { gradeEl -> val grade = gradeEl.asJsonObject - val id = grade.get("id").asLong + val id = grade.get("Id").asLong val categoryId = grade.get("Category").asJsonObject.get("Id").asLong val name = grade.get("Grade").asString val semester = grade.get("Semester").asInt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/ApiError.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/ApiError.kt index d7b21a90..825809f9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/ApiError.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/ApiError.kt @@ -10,7 +10,7 @@ import im.wangchao.mhttp.Response class ApiError(val tag: String, val errorCode: Int) { var profileId: Int? = null - private var throwable: Throwable? = null + var throwable: Throwable? = null private var apiResponse: String? = null private var request: Request? = null private var response: Response? = null diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index 53b9cc9f..0e275be5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -8,6 +8,7 @@ import com.google.gson.JsonObject import im.wangchao.mhttp.Response import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.api.v2.interfaces.EndpointCallback +import pl.szczodrzynski.edziennik.api.v2.librus.Librus import pl.szczodrzynski.edziennik.data.api.AppError.* import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer @@ -30,6 +31,8 @@ import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher import pl.szczodrzynski.edziennik.data.db.modules.teams.Team import pl.szczodrzynski.edziennik.singleOrNull import pl.szczodrzynski.edziennik.toSparseArray +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.values import java.io.InterruptedIOException @@ -41,6 +44,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) var fakeLogin = false + var cancelled = false + val profileId get() = profile?.id ?: -1 @@ -255,6 +260,12 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) } } + fun cancel() { + d("Data", "Cancelled") + cancelled = true + saveData() + } + fun error(tag: String, errorCode: Int, response: Response? = null, throwable: Throwable? = null, apiResponse: JsonObject? = null) { var code = when (throwable) { is UnknownHostException, is SSLException, is InterruptedIOException -> CODE_NO_INTERNET @@ -264,7 +275,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) else -> errorCode } } - callback.onError(ApiError(tag, code).apply { profileId = profile?.id ?: -1 }.withResponse(response).withThrowable(throwable).withApiResponse(apiResponse)) + error(ApiError(tag, code).apply { profileId = profile?.id ?: -1 }.withResponse(response).withThrowable(throwable).withApiResponse(apiResponse)) } fun error(tag: String, errorCode: Int, response: Response? = null, apiResponse: String? = null) { var code = when (null) { @@ -275,9 +286,11 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) else -> errorCode } } - callback.onError(ApiError(tag, code).apply { profileId = profile?.id ?: -1 }.withResponse(response).withApiResponse(apiResponse)) + error(ApiError(tag, code).apply { profileId = profile?.id ?: -1 }.withResponse(response).withApiResponse(apiResponse)) } fun error(apiError: ApiError) { + if (apiError.isCritical) + cancel() callback.onError(apiError) } fun progress(step: Int) {