Moya 8.0.0
func getPersonDetails(personID: String) -> Observable<Person>? {
return Observable.create{ observer in
let _ = self.provider
.request(.getPersonDetails(personID: personID))
.filterSuccessfulStatusCodes()
.mapJSON()
.subscribe(onNext: { response in
observer.onNext(Person(json: JSON(response)))
}, onError: { error in
print(error.localizedDescription)
observer.onError(error)
})
return Disposables.create()
}
}
}
assuming the api will return an error and I can retrieve the error code, how can i retrieve the response json payload?
Hey @ledikari, this feature is planned to be release on Moya 9.0. See #993 :wink:
Hey @ledikari. @pedrovereza is right, in 9.0.0 the underlying case would be getting response as well, but right now you can just cast error to Moya.Error and parse the response data:
.catchError { error in
if let error = error as? Moya.Error, body = error.response?.mapJSON() {}
}
the syntax might be off since I'm writing this from the bus, but the idea should be alright. 馃槄
Let us know if it helped!
@sunshinejr tried your code, but errorResponse.response is nil
do {
let errorResponse = error as? Moya.MoyaError
if let body = try errorResponse?.response?.mapJSON(){
print(body)
}
} catch {
print(error)
}
@ledikari Do you know which case the MoyaError is?
You can use errorResponse?.errorDescription if you'd like.
The reason I'm asking is the MoyaError.underlying and MoyaError.requestMapping return nil for the response property.
@SD10
You can use errorResponse?.errorDescription if you'd like.
i tried printing the errorResponse?.errorDescription and the output is Optional("Response status code was unacceptable: 470.")
how to determine which case of MoyaError it is?
@ledikari It looks like you're receiving a MoyaError.underlying, so the response property will be nil.
You can view the output for MoyaError.errorDescription here.
@SD10 ohh i see, is there any workaround so that we could get the error response body without altering Moya?
@ledikari I don't believe so 馃槶. As others have mentioned this feature will be available in Moya 9.0.0.
@SD10 ugh looks like i need to write alomofire again 馃槶
Yes, we are sorry about that - just the underlying error & body needed in it was so uncommon we didn't really think about it till few months back. But we are closing in for the 9.0.0 version so please take a look at it once it is shipped. Sorry again for the inconvenience 馃槬
@ledikari In my case, I use the 9.0.0-dev branch, and no problem for now, it is very stable. (And you have access to the error body:) )
@ledikari @jeryRazakarison Yes, you can use the version of Moya that is under development if you'd like. I'd only suggest that you fork the repo and point to your own fork instead of relying on our 9.0.0-dev branch as we can't guarantee it will always be stable :wink:
@jeryRazakarison thats an interesting option. I will try to do it.
@sunshinejr any idea on when Moya 9.0 will be released?
edit:
@jeryRazakarison i tried 9 but i see changes. Like what happened to .filterSuccessfulStatusCodes() .mapObject(type: Balance.self) .mapJSON() and .request() ? they all dissapeared
@ledikari Not sure, we are tracking progress in #1075, but slowly getting there. I didn't recommend using our 9.0.0-dev since there might be some breaking changes in the near future, so please use it at your own risk 馃憤
any update
Yes @dimohamdy this is available in Moya 9.0+
Most helpful comment
@sunshinejr tried your code, but errorResponse.response is nil