[APIv2] Fix Librus grades exception. Improve error handling and sync cancellation.

This commit is contained in:
Kuba Szczodrzyński 2019-10-07 20:50:44 +02:00
parent 359fd4efed
commit 1b53c35ec5
7 changed files with 25 additions and 7 deletions

View File

@ -84,6 +84,7 @@ class ApiService : Service() {
apiError.profileId = taskProfileId apiError.profileId = taskProfileId
EventBus.getDefault().post(SyncErrorEvent(apiError)) EventBus.getDefault().post(SyncErrorEvent(apiError))
errorList.add(apiError) errorList.add(apiError)
apiError.throwable?.printStackTrace()
if (apiError.isCritical) { if (apiError.isCritical) {
notification.setCriticalError().post() notification.setCriticalError().post()
taskRunning = false taskRunning = false

View File

@ -133,8 +133,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
} }
override fun cancel() { override fun cancel() {
d(TAG, "Cancelled") data.cancel()
cancelled = true
} }
private fun wrapCallback(callback: EdziennikCallback): EdziennikCallback { private fun wrapCallback(callback: EdziennikCallback): EdziennikCallback {

View File

@ -77,7 +77,8 @@ open class LibrusApi(open val data: DataLibrus) {
data.error(ApiError(tag, EXCEPTION_LIBRUS_API_REQUEST) data.error(ApiError(tag, EXCEPTION_LIBRUS_API_REQUEST)
.withResponse(response) .withResponse(response)
.withThrowable(e) .withThrowable(e)
.withApiResponse(json)) .withApiResponse(json)
.setCritical(false))
} }
} }

View File

@ -41,6 +41,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) { private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) {
Utils.d(TAG, "Using endpoint $endpointId") Utils.d(TAG, "Using endpoint $endpointId")
if (data.cancelled) {
Utils.d(TAG, "Skip endpoint $endpointId; cancelled")
return
}
when (endpointId) { when (endpointId) {
ENDPOINT_LIBRUS_API_ME -> { ENDPOINT_LIBRUS_API_ME -> {
data.startProgress(R.string.edziennik_progress_endpoint_student_info) data.startProgress(R.string.edziennik_progress_endpoint_student_info)

View File

@ -25,7 +25,7 @@ class LibrusApiGrades(override val data: DataLibrus,
grades?.forEach { gradeEl -> grades?.forEach { gradeEl ->
val grade = gradeEl.asJsonObject 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 categoryId = grade.get("Category").asJsonObject.get("Id").asLong
val name = grade.get("Grade").asString val name = grade.get("Grade").asString
val semester = grade.get("Semester").asInt val semester = grade.get("Semester").asInt

View File

@ -10,7 +10,7 @@ import im.wangchao.mhttp.Response
class ApiError(val tag: String, val errorCode: Int) { class ApiError(val tag: String, val errorCode: Int) {
var profileId: Int? = null var profileId: Int? = null
private var throwable: Throwable? = null var throwable: Throwable? = null
private var apiResponse: String? = null private var apiResponse: String? = null
private var request: Request? = null private var request: Request? = null
private var response: Response? = null private var response: Response? = null

View File

@ -8,6 +8,7 @@ import com.google.gson.JsonObject
import im.wangchao.mhttp.Response import im.wangchao.mhttp.Response
import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.api.v2.interfaces.EndpointCallback 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.api.AppError.*
import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement
import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer 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.data.db.modules.teams.Team
import pl.szczodrzynski.edziennik.singleOrNull import pl.szczodrzynski.edziennik.singleOrNull
import pl.szczodrzynski.edziennik.toSparseArray 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.utils.models.Date
import pl.szczodrzynski.edziennik.values import pl.szczodrzynski.edziennik.values
import java.io.InterruptedIOException import java.io.InterruptedIOException
@ -41,6 +44,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
var fakeLogin = false var fakeLogin = false
var cancelled = false
val profileId val profileId
get() = profile?.id ?: -1 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) { fun error(tag: String, errorCode: Int, response: Response? = null, throwable: Throwable? = null, apiResponse: JsonObject? = null) {
var code = when (throwable) { var code = when (throwable) {
is UnknownHostException, is SSLException, is InterruptedIOException -> CODE_NO_INTERNET 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 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) { fun error(tag: String, errorCode: Int, response: Response? = null, apiResponse: String? = null) {
var code = when (null) { var code = when (null) {
@ -275,9 +286,11 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
else -> errorCode 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) { fun error(apiError: ApiError) {
if (apiError.isCritical)
cancel()
callback.onError(apiError) callback.onError(apiError)
} }
fun progress(step: Int) { fun progress(step: Int) {