PeripheralManagerError.swift 2.4 KB

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