NSCv3: better log errors to fragment
This commit is contained in:
parent
c1fca211bf
commit
2cecf28792
6 changed files with 79 additions and 39 deletions
|
@ -5,6 +5,7 @@ import com.google.gson.JsonParser
|
|||
import info.nightscout.sdk.exceptions.DateHeaderOutOfToleranceException
|
||||
import info.nightscout.sdk.exceptions.InvalidAccessTokenException
|
||||
import info.nightscout.sdk.exceptions.InvalidFormatNightscoutException
|
||||
import info.nightscout.sdk.exceptions.InvalidParameterNightscoutException
|
||||
import info.nightscout.sdk.exceptions.UnknownResponseNightscoutException
|
||||
import info.nightscout.sdk.exceptions.UnsuccessfullNightscoutException
|
||||
import info.nightscout.sdk.interfaces.NSAndroidClient
|
||||
|
@ -105,20 +106,22 @@ class NSAndroidClientImpl(
|
|||
val response = api.lastModified()
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper response.body()?.result ?: throw UnsuccessfullNightscoutException()
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getSgvs(): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
||||
|
||||
val response = api.getSgvs()
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteEntry::toSgv).toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getSgvsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
||||
|
||||
|
@ -127,20 +130,22 @@ class NSAndroidClientImpl(
|
|||
val eTagString = response.headers()["ETag"]
|
||||
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result?.map(RemoteEntry::toSgv).toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getSgvsNewerThan(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
||||
|
||||
val response = api.getSgvsNewerThan(from, limit)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteEntry::toSgv).toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun createSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
||||
|
@ -176,7 +181,10 @@ class NSAndroidClientImpl(
|
|||
identifier = null,
|
||||
errorResponse = errorResponse
|
||||
)
|
||||
} else throw UnknownResponseNightscoutException()
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
|
||||
override suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -205,20 +213,22 @@ class NSAndroidClientImpl(
|
|||
deduplicatedIdentifier = null,
|
||||
lastModified = null
|
||||
)
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Long): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
||||
|
||||
val response = api.getTreatmentsNewerThan(createdAt, limit)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteTreatment::toTreatment).toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getTreatmentsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
||||
|
||||
|
@ -226,22 +236,26 @@ class NSAndroidClientImpl(
|
|||
if (response.isSuccessful) {
|
||||
val eTagString = response.headers()["ETag"]
|
||||
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result?.map
|
||||
(RemoteTreatment::toTreatment).toNotNull())
|
||||
} else {
|
||||
return@callWrapper NSAndroidClient.ReadResponse(
|
||||
code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result?.map
|
||||
(RemoteTreatment::toTreatment).toNotNull()
|
||||
)
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getDeviceStatusModifiedSince(from: Long): List<NSDeviceStatus> = callWrapper(dispatcher) {
|
||||
|
||||
val response = api.getDeviceStatusModifiedSince(from)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper response.body()?.result?.map(RemoteDeviceStatus::toNSDeviceStatus).toNotNull()
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun createDeviceStatus(nsDeviceStatus: NSDeviceStatus): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
||||
|
@ -266,10 +280,11 @@ class NSAndroidClientImpl(
|
|||
lastModified = response.body()?.result?.lastModified
|
||||
)
|
||||
} else throw UnknownResponseNightscoutException()
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
||||
|
@ -304,7 +319,10 @@ class NSAndroidClientImpl(
|
|||
identifier = null,
|
||||
errorResponse = errorResponse
|
||||
)
|
||||
} else throw UnknownResponseNightscoutException()
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
|
||||
override suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -333,20 +351,22 @@ class NSAndroidClientImpl(
|
|||
deduplicatedIdentifier = null,
|
||||
lastModified = null
|
||||
)
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getFoods(limit: Long): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
|
||||
|
||||
val response = api.getFoods(limit)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteFood::toNSFood).toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
override suspend fun getFoodsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
|
||||
|
@ -412,10 +432,11 @@ class NSAndroidClientImpl(
|
|||
deduplicatedIdentifier = null,
|
||||
lastModified = null
|
||||
)
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun createProfileStore(remoteProfileStore: JSONObject): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
remoteProfileStore.put("app", "AAPS")
|
||||
|
@ -438,10 +459,11 @@ class NSAndroidClientImpl(
|
|||
lastModified = response.body()?.result?.lastModified
|
||||
)
|
||||
} else throw UnsuccessfullNightscoutException()
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getLastProfileStore(): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
||||
|
||||
|
@ -450,10 +472,11 @@ class NSAndroidClientImpl(
|
|||
val eTagString = response.headers()["ETag"]
|
||||
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result.toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override suspend fun getProfileModifiedSince(from: Long): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
||||
|
@ -463,10 +486,11 @@ class NSAndroidClientImpl(
|
|||
val eTagString = response.headers()["ETag"]
|
||||
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
||||
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result.toNotNull())
|
||||
} else {
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private suspend fun <T> callWrapper(dispatcher: CoroutineDispatcher, block: suspend () -> T): T =
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
package info.nightscout.sdk.exceptions
|
||||
|
||||
class InvalidFormatNightscoutException : NightscoutException()
|
||||
@Suppress("unused")
|
||||
class InvalidFormatNightscoutException : NightscoutException {
|
||||
constructor() : super()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package info.nightscout.sdk.exceptions
|
||||
|
||||
@Suppress("unused")
|
||||
class InvalidParameterNightscoutException : NightscoutException {
|
||||
constructor() : super()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
}
|
|
@ -119,6 +119,7 @@ class NSClientV3Plugin @Inject constructor(
|
|||
|
||||
val JOB_NAME: String = this::class.java.simpleName
|
||||
val REFRESH_INTERVAL = T.secs(30).msecs()
|
||||
const val RECORDS_TO_LOAD = 500L
|
||||
}
|
||||
|
||||
private val disposable = CompositeDisposable()
|
||||
|
|
|
@ -59,9 +59,9 @@ class LoadBgWorker(
|
|||
if ((nsClientV3Plugin.newestDataOnServer?.collections?.entries ?: Long.MAX_VALUE) > lastLoaded) {
|
||||
val sgvs: List<NSSgvV3>
|
||||
val response: NSAndroidClient.ReadResponse<List<NSSgvV3>>?
|
||||
if (isFirstLoad) response = nsAndroidClient.getSgvsNewerThan(lastLoaded, 500)
|
||||
if (isFirstLoad) response = nsAndroidClient.getSgvsNewerThan(lastLoaded, NSClientV3Plugin.RECORDS_TO_LOAD)
|
||||
else {
|
||||
response = nsAndroidClient.getSgvsModifiedSince(lastLoaded, 500)
|
||||
response = nsAndroidClient.getSgvsModifiedSince(lastLoaded, NSClientV3Plugin.RECORDS_TO_LOAD)
|
||||
response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = it }
|
||||
nsClientV3Plugin.storeLastLoadedSrvModified()
|
||||
nsClientV3Plugin.scheduleIrregularExecution() // Idea is to run after 5 min after last BG
|
||||
|
|
|
@ -47,9 +47,9 @@ class LoadTreatmentsWorker(
|
|||
val response: NSAndroidClient.ReadResponse<List<NSTreatment>>?
|
||||
if (isFirstLoad) {
|
||||
val lastLoadedIso = dateUtil.toISOString(lastLoaded)
|
||||
response = nsAndroidClient.getTreatmentsNewerThan(lastLoadedIso, 500)
|
||||
response = nsAndroidClient.getTreatmentsNewerThan(lastLoadedIso, NSClientV3Plugin.RECORDS_TO_LOAD)
|
||||
} else {
|
||||
response = nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, 500)
|
||||
response = nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, NSClientV3Plugin.RECORDS_TO_LOAD)
|
||||
response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = it }
|
||||
nsClientV3Plugin.storeLastLoadedSrvModified()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue