Error.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 `Error` struct provides a convenience for creating custom Alamofire NSErrors.
  26. public struct Error {
  27. /// The domain used for creating all Alamofire errors.
  28. public static let Domain = "com.alamofire.error"
  29. /// The custom error codes generated by Alamofire.
  30. public enum Code: Int {
  31. case inputStreamReadFailed = -6000
  32. case outputStreamWriteFailed = -6001
  33. case contentTypeValidationFailed = -6002
  34. case statusCodeValidationFailed = -6003
  35. case dataSerializationFailed = -6004
  36. case stringSerializationFailed = -6005
  37. case jsonSerializationFailed = -6006
  38. case propertyListSerializationFailed = -6007
  39. }
  40. /// Custom keys contained within certain NSError `userInfo` dictionaries generated by Alamofire.
  41. public struct UserInfoKeys {
  42. /// The content type user info key for a `.ContentTypeValidationFailed` error stored as a `String` value.
  43. public static let ContentType = "ContentType"
  44. /// The status code user info key for a `.StatusCodeValidationFailed` error stored as an `Int` value.
  45. public static let StatusCode = "StatusCode"
  46. }
  47. /**
  48. Creates an `NSError` with the given error code and failure reason.
  49. - parameter code: The error code.
  50. - parameter failureReason: The failure reason.
  51. - returns: An `NSError` with the given error code and failure reason.
  52. */
  53. @available(*, deprecated:3.4.0)
  54. public static func errorWithCode(_ code: Code, failureReason: String) -> NSError {
  55. return errorWithCode(code.rawValue, failureReason: failureReason)
  56. }
  57. /**
  58. Creates an `NSError` with the given error code and failure reason.
  59. - parameter code: The error code.
  60. - parameter failureReason: The failure reason.
  61. - returns: An `NSError` with the given error code and failure reason.
  62. */
  63. @available(*, deprecated:3.4.0)
  64. public static func errorWithCode(_ code: Int, failureReason: String) -> NSError {
  65. let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
  66. return NSError(domain: Domain, code: code, userInfo: userInfo)
  67. }
  68. static func error(domain: String = Error.Domain, code: Code, failureReason: String) -> NSError {
  69. return error(domain: domain, code: code.rawValue, failureReason: failureReason)
  70. }
  71. static func error(domain: String = Error.Domain, code: Int, failureReason: String) -> NSError {
  72. let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
  73. return NSError(domain: domain, code: code, userInfo: userInfo)
  74. }
  75. }