OOResult.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // OOResult.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/24.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import HandyJSON
  10. import Moya
  11. import CocoaLumberjack
  12. public final class OOResult<T:BaseModel> {
  13. var model:T?
  14. var error:OOAppError?
  15. fileprivate var sourceError:Swift.Error?
  16. init(_ result:Result<Response,MoyaError>) {
  17. switch result {
  18. case .success(let resp):
  19. if resp.statusCode == 200 {
  20. self.model = resp.mapObject(T.self)
  21. if let _ = model {
  22. if model?.isSuccess() == false {
  23. self.error = OOAppError.common(type: "APPError", message: model?.message ?? "", statusCode: 10001)
  24. }
  25. }else{
  26. self.error = OOAppError.common(type: "systemError", message: "转换出错", statusCode: 10001)
  27. DDLogError("API接口转换出错")
  28. DDLogError(resp.debugDescription)
  29. }
  30. } else {
  31. if let errResponse = resp.mapObject(BaseErrorResponse.self) {
  32. self.error = OOAppError.common(type: errResponse.type ?? "error", message: "\(errResponse.message ?? "")", statusCode: resp.statusCode)
  33. } else {
  34. self.error = OOAppError.common(type: "networkError", message: "请求出错,status Code \(resp.statusCode) ", statusCode: resp.statusCode)
  35. }
  36. }
  37. break
  38. case .failure(let err):
  39. self.sourceError = err
  40. transError()
  41. break
  42. }
  43. }
  44. func isResultSuccess() -> Bool {
  45. guard let _ = error else {
  46. return true
  47. }
  48. return false
  49. }
  50. fileprivate func transError() {
  51. guard let err = self.sourceError else {
  52. return
  53. }
  54. if err is MoyaError {
  55. let mErr = err as! MoyaError
  56. switch mErr {
  57. case .imageMapping(let resp):
  58. self.error = OOAppError.imageMapping(message: mErr.errorDescription!, statusCode: (mErr.response?.statusCode)!, data: resp.data)
  59. break
  60. case .jsonMapping(let resp):
  61. self.error = OOAppError.jsonMapping(message: mErr.errorDescription!, statusCode: (mErr.response?.statusCode)!, data: resp.data)
  62. break
  63. case .requestMapping(let str):
  64. self.error = OOAppError.requestMapping(str)
  65. break
  66. case .statusCode(let resp):
  67. self.error = OOAppError.statusCode(message: mErr.errorDescription!, statusCode: (mErr.response?.statusCode)!, data: resp.data)
  68. break
  69. case .stringMapping(let resp):
  70. self.error = OOAppError.stringMapping(message: mErr.errorDescription!, statusCode: (mErr.response?.statusCode)!, data: resp.data)
  71. break
  72. case .underlying(let uErr):
  73. self.error = OOAppError.underlying(uErr.0 as! Error)
  74. break
  75. case .objectMapping(let err,let resp):
  76. self.error = OOAppError.objectMapping(err, resp)
  77. break
  78. case .encodableMapping(let err):
  79. self.error = OOAppError.encodableMapping(err)
  80. break
  81. case .parameterEncoding(let err):
  82. self.error = OOAppError.parameterEncoding(err)
  83. break
  84. }
  85. }
  86. }
  87. }