Error.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /**
  39. Creates an `NSError` with the given error code and failure reason.
  40. - parameter code: The error code.
  41. - parameter failureReason: The failure reason.
  42. - returns: An `NSError` with the given error code and failure reason.
  43. */
  44. public static func errorWithCode(code: Code, failureReason: String) -> NSError {
  45. return errorWithCode(code.rawValue, failureReason: failureReason)
  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. public static func errorWithCode(code: Int, failureReason: String) -> NSError {
  54. let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
  55. return NSError(domain: Domain, code: code, userInfo: userInfo)
  56. }
  57. }