Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -828,18 +828,27 @@ extension DeviceDataManager {
metadata.activationType = activationType
let doseId = await newDoseId(for: metadata)

// A remote command reports the pump's error to its sender and sends its own failure notification
let rethrowsErrors = metadata.origin == .remote

do {
try await pumpManager.enactBolus(doseId: doseId, units: units, activationType: activationType)
} catch PumpManagerError.uncertainDelivery {
// Do not generate notification on uncertain delivery error
if rethrowsErrors {
throw PumpManagerError.uncertainDelivery
}
} catch {
if !activationType.isAutomatic, let error = error as? PumpManagerError {
if !activationType.isAutomatic, !rethrowsErrors, let error = error as? PumpManagerError {
do {
try await NotificationManager.sendBolusFailureNotification(for: error, units: units, at: Date(), decisionId: metadata.decisionId, activationType: activationType)
} catch {
log.error("Error sending bolus failure notification %{public}@", String(describing: error))
}
}
if rethrowsErrors {
throw error
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions LoopTests/Managers/DeviceDataManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ final class DeviceDataManagerTests: XCTestCase {
XCTAssertEqual(metadata, DoseMetadata(origin: .user, device: .watch, decisionId: decisionId, activationType: .manualRecommendationAccepted))
}

func testEnactBolusRethrowsPumpErrorsForRemoteOrigin() async throws {
pumpManager.enactBolusError = .uncertainDelivery
do {
try await deviceDataManager.enactBolus(units: 1, metadata: DoseMetadata(origin: .remote), activationType: .manualNoRecommendation)
XCTFail("Expected the pump error")
} catch PumpManagerError.uncertainDelivery {
}

pumpManager.enactBolusError = .deviceState(nil)
do {
try await deviceDataManager.enactBolus(units: 1, metadata: DoseMetadata(origin: .remote), activationType: .manualNoRecommendation)
XCTFail("Expected the pump error")
} catch PumpManagerError.deviceState {
}
pumpManager.enactBolusError = nil
}

func testEnactBolusDoesNotThrowPumpErrorsForOtherOrigins() async throws {
pumpManager.enactBolusError = .uncertainDelivery
try await deviceDataManager.enactBolus(units: 1, metadata: DoseMetadata(origin: .user), activationType: .manualRecommendationAccepted)
pumpManager.enactBolusError = .deviceState(nil)
try await deviceDataManager.enactBolus(units: 1, metadata: DoseMetadata(origin: .user), activationType: .manualRecommendationAccepted)
pumpManager.enactBolusError = nil
}

func testAutomaticDosesStoreMetadataWithTheDecision() async throws {
let decisionId = UUID()
pumpManager.enactedDoseIds = []
Expand Down
4 changes: 3 additions & 1 deletion LoopTests/Mocks/MockPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MockPumpManager: PumpManager {

var enactTempBasalError: PumpManagerError?

var enactBolusError: PumpManagerError?

init() {

}
Expand Down Expand Up @@ -87,7 +89,7 @@ class MockPumpManager: PumpManager {
func enactBolus(doseId: UUID, units: Double, activationType: BolusActivationType, completion: @escaping (PumpManagerError?) -> Void) {
enactedDoseIds.append(doseId)
enactBolusCalled?(units, activationType)
completion(nil)
completion(enactBolusError)
}

func cancelBolus(completion: @escaping (PumpManagerResult<DoseEntry?>) -> Void) {
Expand Down