RileyLinkDeviceError.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // RileyLinkDeviceError.swift
  3. // RileyLinkBLEKit
  4. //
  5. // Copyright © 2018 Pete Schwamb. All rights reserved.
  6. //
  7. public enum RileyLinkDeviceError: Error {
  8. case peripheralManagerError(LocalizedError)
  9. case invalidInput(String)
  10. case writeSizeLimitExceeded(maxLength: Int)
  11. case invalidResponse(Data)
  12. case responseTimeout
  13. case commandsBlocked
  14. case unsupportedCommand(String)
  15. }
  16. extension RileyLinkDeviceError: LocalizedError {
  17. public var errorDescription: String? {
  18. switch self {
  19. case .peripheralManagerError(let error):
  20. return error.errorDescription
  21. case .invalidInput(let input):
  22. return String(format: LocalizedString("Input %@ is invalid", comment: "Invalid input error description (1: input)"), String(describing: input))
  23. case .invalidResponse(let response):
  24. return String(format: LocalizedString("Response %@ is invalid", comment: "Invalid response error description (1: response)"), String(describing: response))
  25. case .writeSizeLimitExceeded(let maxLength):
  26. return String(format: LocalizedString("Data exceeded maximum size of %@ bytes", comment: "Write size limit exceeded error description (1: size limit)"), NumberFormatter.localizedString(from: NSNumber(value: maxLength), number: .none))
  27. case .responseTimeout:
  28. return LocalizedString("Pump did not respond in time", comment: "Response timeout error description")
  29. case .commandsBlocked:
  30. return LocalizedString("RileyLink command did not respond", comment: "commandsBlocked error description")
  31. case .unsupportedCommand(let command):
  32. return String(format: LocalizedString("RileyLink firmware does not support the %@ command", comment: "Unsupported command error description"), String(describing: command))
  33. }
  34. }
  35. public var failureReason: String? {
  36. switch self {
  37. case .peripheralManagerError(let error):
  38. return error.failureReason
  39. default:
  40. return nil
  41. }
  42. }
  43. public var recoverySuggestion: String? {
  44. switch self {
  45. case .peripheralManagerError(let error):
  46. return error.recoverySuggestion
  47. case .commandsBlocked:
  48. return LocalizedString("RileyLink may need to be turned off and back on", comment: "commandsBlocked recovery suggestion")
  49. default:
  50. return nil
  51. }
  52. }
  53. }