From a6a84121da5918789c96a13e8448d603c3f91956 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 2 Nov 2023 20:49:06 +0100 Subject: [PATCH] NSCv3: better error logging --- .../aaps/core/nssdk/NSAndroidClientImpl.kt | 84 +++++++------------ .../DateHeaderOutOfToleranceException.kt | 2 +- .../exceptions/InvalidAccessTokenException.kt | 2 +- .../InvalidFormatNightscoutException.kt | 7 +- .../InvalidParameterNightscoutException.kt | 7 +- .../nssdk/exceptions/NightscoutException.kt | 7 +- .../UnknownResponseNightscoutException.kt | 2 +- .../UnsuccessfulNightscoutException.kt | 3 + .../UnsuccessfullNightscoutException.kt | 8 -- .../nssdk/networking/NSAuthInterceptor.kt | 4 +- 10 files changed, 41 insertions(+), 85 deletions(-) create mode 100644 core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfulNightscoutException.kt delete mode 100644 core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfullNightscoutException.kt diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/NSAndroidClientImpl.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/NSAndroidClientImpl.kt index 6c28f19b63..ffc5a24d4b 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/NSAndroidClientImpl.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/NSAndroidClientImpl.kt @@ -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> = 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> = 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> = 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> = 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> = 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 = 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> = 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> = 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> = 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 callWrapper(dispatcher: CoroutineDispatcher, block: suspend () -> T): T = diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/DateHeaderOutOfToleranceException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/DateHeaderOutOfToleranceException.kt index 3682dd65be..2ce3b399ab 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/DateHeaderOutOfToleranceException.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/DateHeaderOutOfToleranceException.kt @@ -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) diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidAccessTokenException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidAccessTokenException.kt index 2fc0ccd4ca..26046c1958 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidAccessTokenException.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidAccessTokenException.kt @@ -1,3 +1,3 @@ package app.aaps.core.nssdk.exceptions -class InvalidAccessTokenException : NightscoutException() +class InvalidAccessTokenException(message: String) : NightscoutException(message) diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidFormatNightscoutException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidFormatNightscoutException.kt index bf9d55c28a..5f088f3324 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidFormatNightscoutException.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidFormatNightscoutException.kt @@ -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) \ No newline at end of file diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidParameterNightscoutException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidParameterNightscoutException.kt index 155530aaad..a78ce929c2 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidParameterNightscoutException.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/InvalidParameterNightscoutException.kt @@ -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) \ No newline at end of file diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/NightscoutException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/NightscoutException.kt index 3975fa2ce6..a0c0fceab2 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/NightscoutException.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/NightscoutException.kt @@ -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) \ No newline at end of file diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnknownResponseNightscoutException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnknownResponseNightscoutException.kt index f676bdaec6..c909b01597 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnknownResponseNightscoutException.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnknownResponseNightscoutException.kt @@ -1,3 +1,3 @@ package app.aaps.core.nssdk.exceptions -class UnknownResponseNightscoutException : NightscoutException() +class UnknownResponseNightscoutException(override val message: String) : NightscoutException(message) diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfulNightscoutException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfulNightscoutException.kt new file mode 100644 index 0000000000..046e7f95ba --- /dev/null +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfulNightscoutException.kt @@ -0,0 +1,3 @@ +package app.aaps.core.nssdk.exceptions + +class UnsuccessfulNightscoutException(message: String) : NightscoutException(message) \ No newline at end of file diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfullNightscoutException.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfullNightscoutException.kt deleted file mode 100644 index b342c109c5..0000000000 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/exceptions/UnsuccessfullNightscoutException.kt +++ /dev/null @@ -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) -} diff --git a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/networking/NSAuthInterceptor.kt b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/networking/NSAuthInterceptor.kt index 781321a4ea..1816169e9d 100644 --- a/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/networking/NSAuthInterceptor.kt +++ b/core/nssdk/src/main/kotlin/app/aaps/core/nssdk/networking/NSAuthInterceptor.kt @@ -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") } } }