PumpOpsError.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // PumpOpsError.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 5/9/17.
  6. // Copyright © 2017 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import RileyLinkBLEKit
  10. /// An error that occurs during a command run
  11. ///
  12. /// - command: The error took place during the command sequence
  13. /// - arguments: The error took place during the argument sequence
  14. public enum PumpCommandError: Error {
  15. case command(PumpOpsError)
  16. case arguments(PumpOpsError)
  17. }
  18. public enum PumpOpsError: Error {
  19. case bolusInProgress
  20. case couldNotDecode(rx: Data, during: CustomStringConvertible)
  21. case crosstalk(PumpMessage, during: CustomStringConvertible)
  22. case deviceError(LocalizedError)
  23. case noResponse(during: CustomStringConvertible)
  24. case pumpError(PumpErrorCode)
  25. case pumpSuspended
  26. case rfCommsFailure(String)
  27. case unexpectedResponse(PumpMessage, from: PumpMessage)
  28. case unknownPumpErrorCode(UInt8)
  29. case unknownPumpModel(String)
  30. case unknownResponse(rx: Data, during: CustomStringConvertible)
  31. }
  32. extension PumpOpsError: LocalizedError {
  33. public var errorDescription: String? {
  34. switch self {
  35. case .bolusInProgress:
  36. return nil
  37. case .couldNotDecode:
  38. return LocalizedString("Decoding Error", comment: "Error description")
  39. case .crosstalk:
  40. return nil
  41. case .deviceError:
  42. return LocalizedString("Device Error", comment: "Error description")
  43. case .noResponse:
  44. return nil
  45. case .pumpError:
  46. return LocalizedString("Pump Error", comment: "Error description")
  47. case .pumpSuspended:
  48. return nil
  49. case .rfCommsFailure:
  50. return nil
  51. case .unexpectedResponse:
  52. return nil
  53. case .unknownPumpErrorCode:
  54. return nil
  55. case .unknownPumpModel:
  56. return nil
  57. case .unknownResponse:
  58. return nil
  59. }
  60. }
  61. public var failureReason: String? {
  62. switch self {
  63. case .bolusInProgress:
  64. return LocalizedString("A bolus is already in progress", comment: "Communications error for a bolus currently running")
  65. case .couldNotDecode(rx: let data, during: let during):
  66. return String(format: LocalizedString("Invalid response during %1$@: %2$@", comment: "Format string for failure reason. (1: The operation being performed) (2: The response data)"), String(describing: during), data.hexadecimalString)
  67. case .crosstalk:
  68. return LocalizedString("Comms with another pump detected", comment: "")
  69. case .noResponse:
  70. return LocalizedString("Pump did not respond", comment: "")
  71. case .pumpSuspended:
  72. return LocalizedString("Pump is suspended", comment: "")
  73. case .rfCommsFailure(let msg):
  74. return msg
  75. case .unexpectedResponse:
  76. return LocalizedString("Pump responded unexpectedly", comment: "")
  77. case .unknownPumpErrorCode(let code):
  78. return String(format: LocalizedString("Unknown pump error code: %1$@", comment: "The format string description of an unknown pump error code. (1: The specific error code raw value)"), String(describing: code))
  79. case .unknownPumpModel(let model):
  80. return String(format: LocalizedString("Unknown pump model: %@", comment: ""), model)
  81. case .unknownResponse(rx: let data, during: let during):
  82. return String(format: LocalizedString("Unknown response during %1$@: %2$@", comment: "Format string for an unknown response. (1: The operation being performed) (2: The response data)"), String(describing: during), data.hexadecimalString)
  83. case .pumpError(let errorCode):
  84. return String(describing: errorCode)
  85. case .deviceError(let error):
  86. return [error.errorDescription, error.failureReason].compactMap({ $0 }).joined(separator: ": ")
  87. }
  88. }
  89. public var recoverySuggestion: String? {
  90. switch self {
  91. case .pumpError(let errorCode):
  92. return errorCode.recoverySuggestion
  93. case .deviceError(let error):
  94. return error.recoverySuggestion
  95. default:
  96. return nil
  97. }
  98. }
  99. public var helpAnchor: String? {
  100. switch self {
  101. case .deviceError(let error):
  102. return error.helpAnchor
  103. default:
  104. return nil
  105. }
  106. }
  107. }
  108. extension PumpCommandError: LocalizedError {
  109. public var errorDescription: String? {
  110. switch self {
  111. case .arguments(let error):
  112. return error.errorDescription
  113. case .command(let error):
  114. return error.errorDescription
  115. }
  116. }
  117. public var failureReason: String? {
  118. switch self {
  119. case .arguments(let error):
  120. return error.failureReason
  121. case .command(let error):
  122. return error.failureReason
  123. }
  124. }
  125. public var recoverySuggestion: String? {
  126. switch self {
  127. case .arguments(let error):
  128. return error.recoverySuggestion
  129. case .command(let error):
  130. return error.recoverySuggestion
  131. }
  132. }
  133. public var helpAnchor: String? {
  134. switch self {
  135. case .arguments(let error):
  136. return error.helpAnchor
  137. case .command(let error):
  138. return error.helpAnchor
  139. }
  140. }
  141. }