PumpOpsError.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 LocalizedString("A bolus is already in progress", comment: "Communications error for a bolus currently running")
  37. case .couldNotDecode(rx: let data, during: let during):
  38. 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)
  39. case .crosstalk:
  40. return LocalizedString("Comms with another pump detected", comment: "")
  41. case .noResponse:
  42. return LocalizedString("Pump did not respond", comment: "")
  43. case .pumpSuspended:
  44. return LocalizedString("Pump is suspended", comment: "")
  45. case .rfCommsFailure(let msg):
  46. return msg
  47. case .unexpectedResponse(let response, _):
  48. return String(format: LocalizedString("Unexpected response %1$@", comment: "Format string for an unexpectedResponse. (2: The response)"), String(describing: response))
  49. case .unknownPumpErrorCode(let code):
  50. 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))
  51. case .unknownPumpModel(let model):
  52. return String(format: LocalizedString("Unknown pump model: %@", comment: ""), model)
  53. case .unknownResponse(rx: let data, during: let during):
  54. 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)
  55. case .pumpError(let errorCode):
  56. return String(describing: errorCode)
  57. case .deviceError(let error):
  58. return [error.errorDescription, error.failureReason].compactMap({ $0 }).joined(separator: ": ")
  59. }
  60. }
  61. public var recoverySuggestion: String? {
  62. switch self {
  63. case .pumpError(let errorCode):
  64. return errorCode.recoverySuggestion
  65. case .deviceError(let error):
  66. return error.recoverySuggestion
  67. default:
  68. return nil
  69. }
  70. }
  71. public var helpAnchor: String? {
  72. switch self {
  73. case .deviceError(let error):
  74. return error.helpAnchor
  75. default:
  76. return nil
  77. }
  78. }
  79. }
  80. extension PumpCommandError: LocalizedError {
  81. public var errorDescription: String? {
  82. switch self {
  83. case .arguments(let error):
  84. return error.errorDescription
  85. case .command(let error):
  86. return error.errorDescription
  87. }
  88. }
  89. public var failureReason: String? {
  90. switch self {
  91. case .arguments(let error):
  92. return error.failureReason
  93. case .command(let error):
  94. return error.failureReason
  95. }
  96. }
  97. public var recoverySuggestion: String? {
  98. switch self {
  99. case .arguments(let error):
  100. return error.recoverySuggestion
  101. case .command(let error):
  102. return error.recoverySuggestion
  103. }
  104. }
  105. public var helpAnchor: String? {
  106. switch self {
  107. case .arguments(let error):
  108. return error.helpAnchor
  109. case .command(let error):
  110. return error.helpAnchor
  111. }
  112. }
  113. }