Error.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Error.swift
  3. //
  4. // Copyright (c) 2014-2016 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. /// The domain used for creating all Alamofire errors.
  26. public let ErrorDomain = "org.alamofire.error"
  27. /// The custom error codes generated by Alamofire.
  28. public enum ErrorCode: Int {
  29. case inputStreamReadFailed = -6000
  30. case outputStreamWriteFailed = -6001
  31. case contentTypeValidationFailed = -6002
  32. case statusCodeValidationFailed = -6003
  33. case dataSerializationFailed = -6004
  34. case stringSerializationFailed = -6005
  35. case jsonSerializationFailed = -6006
  36. case propertyListSerializationFailed = -6007
  37. }
  38. // MARK: -
  39. /// Custom keys contained within certain NSError `userInfo` dictionaries generated by Alamofire.
  40. public struct ErrorUserInfoKeys {
  41. /// The content type user info key for a `.ContentTypeValidationFailed` error stored as a `String` value.
  42. public static let ContentType = "ContentType"
  43. /// The status code user info key for a `.StatusCodeValidationFailed` error stored as an `Int` value.
  44. public static let StatusCode = "StatusCode"
  45. }
  46. // MARK: -
  47. extension NSError {
  48. convenience init(domain: String = ErrorDomain, code: ErrorCode, failureReason: String) {
  49. self.init(domain: domain, code: code.rawValue, failureReason: failureReason)
  50. }
  51. convenience init(domain: String = ErrorDomain, code: Int, failureReason: String) {
  52. let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
  53. self.init(domain: domain, code: code, userInfo: userInfo)
  54. }
  55. }