RileyLinkDeviceError.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // RileyLinkDeviceError.swift
  3. // RileyLinkBLEKit
  4. //
  5. // Copyright © 2018 Pete Schwamb. All rights reserved.
  6. //
  7. public enum RileyLinkDeviceError: Error {
  8. case peripheralManagerError(PeripheralManagerError)
  9. case invalidInput(String)
  10. case writeSizeLimitExceeded(maxLength: Int)
  11. case invalidResponse(Data)
  12. case responseTimeout
  13. case unsupportedCommand(String)
  14. }
  15. extension RileyLinkDeviceError: LocalizedError {
  16. public var errorDescription: String? {
  17. switch self {
  18. case .peripheralManagerError(let error):
  19. return error.errorDescription
  20. case .invalidInput(let input):
  21. return String(format: LocalizedString("Input %@ is invalid", comment: "Invalid input error description (1: input)"), String(describing: input))
  22. case .invalidResponse(let response):
  23. return String(format: LocalizedString("Response %@ is invalid", comment: "Invalid response error description (1: response)"), String(describing: response))
  24. case .writeSizeLimitExceeded(let maxLength):
  25. 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))
  26. case .responseTimeout:
  27. return LocalizedString("Pump did not respond in time", comment: "Response timeout error description")
  28. case .unsupportedCommand(let command):
  29. return String(format: LocalizedString("RileyLink firmware does not support the %@ command", comment: "Unsupported command error description"), String(describing: command))
  30. }
  31. }
  32. public var failureReason: String? {
  33. switch self {
  34. case .peripheralManagerError(let error):
  35. return error.failureReason
  36. default:
  37. return nil
  38. }
  39. }
  40. public var recoverySuggestion: String? {
  41. switch self {
  42. case .peripheralManagerError(let error):
  43. return error.recoverySuggestion
  44. default:
  45. return nil
  46. }
  47. }
  48. }