ALTServerError+Conveniences.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // ALTServerError+Conveniences.swift
  3. // AltKit
  4. //
  5. // Created by Riley Testut on 6/4/20.
  6. // Copyright © 2020 Riley Testut. All rights reserved.
  7. //
  8. import Foundation
  9. public extension ALTServerError
  10. {
  11. init<E: Error>(_ error: E)
  12. {
  13. switch error
  14. {
  15. case let error as ALTServerError: self = error
  16. case let error as ALTServerConnectionError:
  17. self = ALTServerError(.connectionFailed, underlyingError: error)
  18. case is DecodingError: self = ALTServerError(.invalidRequest, underlyingError: error)
  19. case is EncodingError: self = ALTServerError(.invalidResponse, underlyingError: error)
  20. case let error as NSError:
  21. var userInfo = error.userInfo
  22. if !userInfo.keys.contains(NSUnderlyingErrorKey)
  23. {
  24. // Assign underlying error (if there isn't already one).
  25. userInfo[NSUnderlyingErrorKey] = error
  26. }
  27. self = ALTServerError(.underlyingError, userInfo: userInfo)
  28. }
  29. }
  30. init<E: Error>(_ code: ALTServerError.Code, underlyingError: E)
  31. {
  32. self = ALTServerError(code, userInfo: [NSUnderlyingErrorKey: underlyingError])
  33. }
  34. }