|
|
@@ -423,20 +423,26 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
// Fetch glucose asynchronously
|
|
|
let glucose = try await fetchGlucose(predicate: NSPredicate.predicateForOneHourAgo, fetchLimit: 6)
|
|
|
|
|
|
+ var invalidGlucoseError: String?
|
|
|
+
|
|
|
// Perform the context-related checks and actions
|
|
|
let isValidGlucoseData = await privateContext.perform { [weak self] in
|
|
|
guard let self else { return false }
|
|
|
|
|
|
guard glucose.count > 2 else {
|
|
|
debug(.apsManager, "Not enough glucose data")
|
|
|
- self.processError(APSError.glucoseError(message: String(localized: "Not enough glucose data")))
|
|
|
+ invalidGlucoseError =
|
|
|
+ String(
|
|
|
+ localized: "Not enough glucose data. You need at least three glucose readings in the last six hours to run the algorithm."
|
|
|
+ )
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
let dateOfLastGlucose = glucose.first?.date
|
|
|
guard dateOfLastGlucose ?? Date() >= Date().addingTimeInterval(-12.minutes.timeInterval) else {
|
|
|
debug(.apsManager, "Glucose data is stale")
|
|
|
- self.processError(APSError.glucoseError(message: String(localized: "Glucose data is stale")))
|
|
|
+ invalidGlucoseError =
|
|
|
+ String(localized: "Glucose data is stale. The most recent glucose reading is from more than 12 minutes ago.")
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
@@ -468,7 +474,15 @@ final class BaseAPSManager: APSManager, Injectable {
|
|
|
}
|
|
|
}
|
|
|
} catch {
|
|
|
- throw APSError.apsError(message: "Error determining basal: \(error.localizedDescription)")
|
|
|
+ // if we have a glucose validation error we might still run
|
|
|
+ // determineBasal to try to get IoB and CoB updates but we
|
|
|
+ // know that it will fail, so the invalidGlucoseError always
|
|
|
+ // takes priority
|
|
|
+ if let invalidGlucoseError = invalidGlucoseError {
|
|
|
+ throw APSError.apsError(message: invalidGlucoseError)
|
|
|
+ } else {
|
|
|
+ throw APSError.apsError(message: "Error determining basal: \(error.localizedDescription)")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|