You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

  1. //
  2. // Promise+BridgeError.swift
  3. // then
  4. //
  5. // Created by Sacha Durand Saint Omer on 24/02/2017.
  6. // Copyright © 2017 s4cha. All rights reserved.
  7. //
  8. import Foundation
  9. public extension Promise {
  10. public func bridgeError(to myError: Error) -> Promise<T> {
  11. let p = newLinkedPromise()
  12. syncStateWithCallBacks(
  13. success: p.fulfill,
  14. failure: { _ in
  15. p.reject(myError)
  16. },
  17. progress: p.setProgress)
  18. return p
  19. }
  20. public func bridgeError(_ errorType: Error, to myError: Error) -> Promise<T> {
  21. let p = newLinkedPromise()
  22. syncStateWithCallBacks(
  23. success: p.fulfill,
  24. failure: { e in
  25. if e._code == errorType._code && e._domain == errorType._domain {
  26. p.reject(myError)
  27. } else {
  28. p.reject(e)
  29. }
  30. },
  31. progress: p.setProgress)
  32. return p
  33. }
  34. public func bridgeError(_ block:@escaping (Error) throws -> Void) -> Promise<T> {
  35. let p = newLinkedPromise()
  36. syncStateWithCallBacks(
  37. success: p.fulfill,
  38. failure: { e in
  39. do {
  40. try block(e)
  41. } catch {
  42. p.reject(error)
  43. }
  44. },
  45. progress: p.setProgress)
  46. return p
  47. }
  48. }