NSCv3: better error logging
This commit is contained in:
parent
2b351c35ff
commit
a6a84121da
10 changed files with 41 additions and 85 deletions
core/nssdk/src/main/kotlin/app/aaps/core/nssdk
NSAndroidClientImpl.kt
exceptions
DateHeaderOutOfToleranceException.ktInvalidAccessTokenException.ktInvalidFormatNightscoutException.ktInvalidParameterNightscoutException.ktNightscoutException.ktUnknownResponseNightscoutException.ktUnsuccessfulNightscoutException.ktUnsuccessfullNightscoutException.kt
networking
|
@ -7,7 +7,7 @@ import app.aaps.core.nssdk.exceptions.InvalidAccessTokenException
|
|||
import app.aaps.core.nssdk.exceptions.InvalidFormatNightscoutException
|
||||
import app.aaps.core.nssdk.exceptions.InvalidParameterNightscoutException
|
||||
import app.aaps.core.nssdk.exceptions.UnknownResponseNightscoutException
|
||||
import app.aaps.core.nssdk.exceptions.UnsuccessfullNightscoutException
|
||||
import app.aaps.core.nssdk.exceptions.UnsuccessfulNightscoutException
|
||||
import app.aaps.core.nssdk.interfaces.NSAndroidClient
|
||||
import app.aaps.core.nssdk.localmodel.Status
|
||||
import app.aaps.core.nssdk.localmodel.devicestatus.NSDeviceStatus
|
||||
|
@ -108,11 +108,11 @@ class NSAndroidClientImpl(
|
|||
|
||||
val response = api.lastModified()
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper response.body()?.result ?: throw UnsuccessfullNightscoutException()
|
||||
return@callWrapper response.body()?.result ?: throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun getSgvs(): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
||||
|
@ -127,7 +127,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun getSgvsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
||||
|
@ -144,7 +144,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun getSgvsNewerThan(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
||||
|
@ -159,7 +159,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun createSgv(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -197,7 +197,7 @@ class NSAndroidClientImpl(
|
|||
errorResponse = errorResponse ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(errorResponse ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(errorResponse ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -206,19 +206,11 @@ class NSAndroidClientImpl(
|
|||
nsSgvV3.utcOffset = null
|
||||
nsSgvV3.date = null
|
||||
val remoteEntry = nsSgvV3.toRemoteEntry()
|
||||
val identifier = remoteEntry.identifier ?: throw InvalidFormatNightscoutException()
|
||||
val identifier = remoteEntry.identifier ?: throw InvalidFormatNightscoutException("Invalid format")
|
||||
val response =
|
||||
if (nsSgvV3.isValid) api.updateEntry(remoteEntry, identifier)
|
||||
else api.deleteEntry(identifier)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
isDeduplication = false,
|
||||
deduplicatedIdentifier = null,
|
||||
lastModified = null
|
||||
)
|
||||
} else if (response.code() == 404) { // not found
|
||||
if (response.isSuccessful || response.code() == 404) { // OK or not found
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
|
@ -233,7 +225,7 @@ class NSAndroidClientImpl(
|
|||
errorResponse = response.errorBody()?.string() ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Int): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
||||
|
@ -248,7 +240,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun getTreatmentsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
||||
|
@ -264,7 +256,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun getDeviceStatusModifiedSince(from: Long): List<NSDeviceStatus> = callWrapper(dispatcher) {
|
||||
|
@ -275,7 +267,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun createDeviceStatus(nsDeviceStatus: NSDeviceStatus): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -291,7 +283,7 @@ class NSAndroidClientImpl(
|
|||
deduplicatedIdentifier = response.body()?.deduplicatedIdentifier,
|
||||
lastModified = response.body()?.lastModified
|
||||
)
|
||||
} else throw UnknownResponseNightscoutException()
|
||||
} else throw UnknownResponseNightscoutException("Unsuccessful")
|
||||
} else return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
|
@ -301,7 +293,7 @@ class NSAndroidClientImpl(
|
|||
|
||||
override suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
||||
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException()
|
||||
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException("Invalid format")
|
||||
remoteTreatment.app = "AAPS"
|
||||
val response = api.createTreatment(remoteTreatment)
|
||||
val errorResponse = response.errorBody()?.string()
|
||||
|
@ -333,7 +325,7 @@ class NSAndroidClientImpl(
|
|||
errorResponse = errorResponse ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(errorResponse ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(errorResponse ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -341,20 +333,12 @@ class NSAndroidClientImpl(
|
|||
// following cannot be updated
|
||||
nsTreatment.utcOffset = null
|
||||
nsTreatment.date = null
|
||||
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException()
|
||||
val identifier = remoteTreatment.identifier ?: throw InvalidFormatNightscoutException()
|
||||
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException("Invalid format")
|
||||
val identifier = remoteTreatment.identifier ?: throw InvalidFormatNightscoutException("Invalid format")
|
||||
val response =
|
||||
if (nsTreatment.isValid) api.updateTreatment(remoteTreatment, identifier)
|
||||
else api.deleteTreatment(identifier)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
isDeduplication = false,
|
||||
deduplicatedIdentifier = null,
|
||||
lastModified = null
|
||||
)
|
||||
} else if (response.code() == 404) { // not found
|
||||
if (response.isSuccessful || response.code() == 404) { // OK or not found
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
|
@ -369,7 +353,7 @@ class NSAndroidClientImpl(
|
|||
errorResponse = response.errorBody()?.string() ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun getFoods(limit: Int): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
|
||||
|
@ -384,7 +368,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -414,7 +398,7 @@ class NSAndroidClientImpl(
|
|||
deduplicatedIdentifier = response.body()?.deduplicatedIdentifier,
|
||||
lastModified = response.body()?.lastModified
|
||||
)
|
||||
} else throw UnsuccessfullNightscoutException()
|
||||
} else throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
} else if (response.code() in 400..499) {
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
|
@ -422,25 +406,17 @@ class NSAndroidClientImpl(
|
|||
errorResponse = response.errorBody()?.string() ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun updateFood(nsFood: NSFood): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
||||
val remoteFood = nsFood.toRemoteFood()
|
||||
val identifier = nsFood.identifier ?: throw InvalidFormatNightscoutException()
|
||||
val identifier = nsFood.identifier ?: throw InvalidFormatNightscoutException("Invalid format")
|
||||
val response =
|
||||
if (nsFood.isValid) api.updateFood(remoteFood, identifier)
|
||||
else api.deleteFood(identifier)
|
||||
if (response.isSuccessful) {
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
isDeduplication = false,
|
||||
deduplicatedIdentifier = null,
|
||||
lastModified = null
|
||||
)
|
||||
} else if (response.code() == 404) { // not found
|
||||
if (response.isSuccessful || response.code() == 404) { // OK or not found
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
identifier = null,
|
||||
|
@ -455,7 +431,7 @@ class NSAndroidClientImpl(
|
|||
errorResponse = response.errorBody()?.string() ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun createProfileStore(remoteProfileStore: JSONObject): CreateUpdateResponse = callWrapper(dispatcher) {
|
||||
|
@ -470,7 +446,7 @@ class NSAndroidClientImpl(
|
|||
deduplicatedIdentifier = response.body()?.deduplicatedIdentifier,
|
||||
lastModified = response.body()?.lastModified
|
||||
)
|
||||
} else throw UnsuccessfullNightscoutException()
|
||||
} else throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
} else if (response.code() in 400..499) {
|
||||
return@callWrapper CreateUpdateResponse(
|
||||
response = response.code(),
|
||||
|
@ -478,7 +454,7 @@ class NSAndroidClientImpl(
|
|||
errorResponse = response.errorBody()?.string() ?: response.message()
|
||||
)
|
||||
} else
|
||||
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
}
|
||||
|
||||
override suspend fun getLastProfileStore(): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
||||
|
@ -491,7 +467,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
override suspend fun getProfileModifiedSince(from: Long): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
||||
|
@ -504,7 +480,7 @@ class NSAndroidClientImpl(
|
|||
} else if (response.code() in 400..499)
|
||||
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
||||
else
|
||||
throw UnsuccessfullNightscoutException()
|
||||
throw UnsuccessfulNightscoutException("Unsuccessful")
|
||||
}
|
||||
|
||||
private suspend fun <T> callWrapper(dispatcher: CoroutineDispatcher, block: suspend () -> T): T =
|
||||
|
|
|
@ -6,4 +6,4 @@ package app.aaps.core.nssdk.exceptions
|
|||
* In practice this will happen if the server time and the phone time are off.
|
||||
*
|
||||
*/
|
||||
class DateHeaderOutOfToleranceException : NightscoutException()
|
||||
class DateHeaderOutOfToleranceException(message: String) : NightscoutException(message)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package app.aaps.core.nssdk.exceptions
|
||||
|
||||
class InvalidAccessTokenException : NightscoutException()
|
||||
class InvalidAccessTokenException(message: String) : NightscoutException(message)
|
||||
|
|
|
@ -1,9 +1,4 @@
|
|||
package app.aaps.core.nssdk.exceptions
|
||||
|
||||
@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)
|
||||
}
|
||||
class InvalidFormatNightscoutException(message: String) : NightscoutException(message)
|
|
@ -1,9 +1,4 @@
|
|||
package app.aaps.core.nssdk.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)
|
||||
}
|
||||
class InvalidParameterNightscoutException(message: String) : NightscoutException(message)
|
|
@ -2,9 +2,4 @@ package app.aaps.core.nssdk.exceptions
|
|||
|
||||
import java.io.IOException
|
||||
|
||||
abstract class NightscoutException : IOException {
|
||||
constructor() : super()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
abstract class NightscoutException(message: String, cause: Throwable? = null) : IOException(message, cause)
|
|
@ -1,3 +1,3 @@
|
|||
package app.aaps.core.nssdk.exceptions
|
||||
|
||||
class UnknownResponseNightscoutException : NightscoutException()
|
||||
class UnknownResponseNightscoutException(override val message: String) : NightscoutException(message)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package app.aaps.core.nssdk.exceptions
|
||||
|
||||
class UnsuccessfulNightscoutException(message: String) : NightscoutException(message)
|
|
@ -1,8 +0,0 @@
|
|||
package app.aaps.core.nssdk.exceptions
|
||||
|
||||
class UnsuccessfullNightscoutException : NightscoutException {
|
||||
constructor() : super()
|
||||
constructor(message: String) : super(message)
|
||||
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
}
|
|
@ -49,7 +49,7 @@ internal class NSAuthInterceptor(private val refreshToken: String, private val r
|
|||
|
||||
return when {
|
||||
authResponseResponse == null -> initialResponse
|
||||
authResponseResponse.code() in listOf(401, 403) -> throw InvalidAccessTokenException()
|
||||
authResponseResponse.code() in listOf(401, 403) -> throw InvalidAccessTokenException("Invalid access token")
|
||||
authResponseResponse.code() != 200 -> initialResponse
|
||||
else -> {
|
||||
authResponseResponse.body()?.token?.let { jwtToken = it }
|
||||
|
@ -62,7 +62,7 @@ internal class NSAuthInterceptor(private val refreshToken: String, private val r
|
|||
private fun testCanRefresh(initialResponse: Response) {
|
||||
// Todo: use proper reason code once it is supplied by remote
|
||||
if (initialResponse.body?.string()?.contains(MESSAGE_DATE_HEADER_OUT_OF_TOLERANCE) == true) {
|
||||
throw DateHeaderOutOfToleranceException()
|
||||
throw DateHeaderOutOfToleranceException("Data header out of tolerance")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue