Reputation: 923
I'm using RxAlamorefire to handle network task in my app.
My problem is: When I make a request and it return json. If json has a key "error" I need emit error notification instead of onNext notification.
My code is like this one:
let observable = RxAlamofire.json(.get, url, parameters: nil, encoding: URLEncoding.default, headers: header)
.map { (json) -> SomeObject? in
//Should check json maybe not in ".map" to see if the json contain "error" then emit onError notification.
return Mapper<SomeObject>().map(JSONObject: json)
}
Upvotes: 0
Views: 366
Reputation: 13651
Within map
, you can use the throw
keyword to send out and error
let observable = request.map { (json) -> SomeObject in
if let error = json["error"] as? [AnyHashable: Any] {
throw Mapper<ErrorObject>().map(JSONObject: error)
} else {
// regular deserialization
}
}
This will result in an observable emitting an error of type ErrorObject
when the json contains the error
key.
Upvotes: 1