Error.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Error.swift
  2. //
  3. // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. /// The `Error` struct provides a convenience for creating custom Alamofire NSErrors.
  24. public struct Error {
  25. /// The domain used for creating all Alamofire errors.
  26. public static let Domain = "com.alamofire.error"
  27. /// The custom error codes generated by Alamofire.
  28. public enum Code: 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. /// Custom keys contained within certain NSError `userInfo` dictionaries generated by Alamofire.
  39. public struct UserInfoKeys {
  40. /// The content type user info key for a `.ContentTypeValidationFailed` error stored as a `String` value.
  41. public static let ContentType = "ContentType"
  42. /// The status code user info key for a `.StatusCodeValidationFailed` error stored as an `Int` value.
  43. public static let StatusCode = "StatusCode"
  44. }
  45. /**
  46. Creates an `NSError` with the given error code and failure reason.
  47. - parameter code: The error code.
  48. - parameter failureReason: The failure reason.
  49. - returns: An `NSError` with the given error code and failure reason.
  50. */
  51. @available(*, deprecated=3.4.0)
  52. public static func errorWithCode(code: Code, failureReason: String) -> NSError {
  53. return errorWithCode(code.rawValue, failureReason: failureReason)
  54. }
  55. /**
  56. Creates an `NSError` with the given error code and failure reason.
  57. - parameter code: The error code.
  58. - parameter failureReason: The failure reason.
  59. - returns: An `NSError` with the given error code and failure reason.
  60. */
  61. @available(*, deprecated=3.4.0)
  62. public static func errorWithCode(code: Int, failureReason: String) -> NSError {
  63. let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
  64. return NSError(domain: Domain, code: code, userInfo: userInfo)
  65. }
  66. static func error(domain domain: String = Error.Domain, code: Code, failureReason: String) -> NSError {
  67. return error(domain: domain, code: code.rawValue, failureReason: failureReason)
  68. }
  69. static func error(domain domain: String = Error.Domain, code: Int, failureReason: String) -> NSError {
  70. let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
  71. return NSError(domain: domain, code: code, userInfo: userInfo)
  72. }
  73. }