PeripheralManagerError.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // PeripheralManagerError.swift
  3. // RileyLinkBLEKit
  4. //
  5. // Copyright © 2017 Pete Schwamb. All rights reserved.
  6. //
  7. import CoreBluetooth
  8. public enum PeripheralManagerError: Error {
  9. case cbPeripheralError(Error)
  10. case notReady
  11. case timeout
  12. case unknownCharacteristic
  13. }
  14. extension PeripheralManagerError: LocalizedError {
  15. public var errorDescription: String? {
  16. switch self {
  17. case .cbPeripheralError(let error):
  18. return error.localizedDescription
  19. case .notReady:
  20. return LocalizedString("RileyLink is not connected", comment: "Not ready error description")
  21. case .timeout:
  22. return LocalizedString("RileyLink did not respond in time", comment: "Timeout error description")
  23. case .unknownCharacteristic:
  24. return LocalizedString("Unknown characteristic", comment: "Error description")
  25. }
  26. }
  27. public var failureReason: String? {
  28. switch self {
  29. case .cbPeripheralError(let error as NSError):
  30. return error.localizedFailureReason
  31. case .unknownCharacteristic:
  32. return LocalizedString("The RileyLink was temporarily disconnected", comment: "Failure reason: unknown peripheral characteristic")
  33. default:
  34. return nil
  35. }
  36. }
  37. public var recoverySuggestion: String? {
  38. switch self {
  39. case .cbPeripheralError(let error as NSError):
  40. return error.localizedRecoverySuggestion
  41. case .unknownCharacteristic:
  42. return LocalizedString("Make sure the device is nearby, and the issue should resolve automatically", comment: "Recovery suggestion for unknown peripheral characteristic")
  43. default:
  44. return nil
  45. }
  46. }
  47. }