freshOS / ws-deprecated

⚠️ Deprecated - (in favour of Networking) :cloud: Elegantly connect to a JSON api. (Alamofire + Promises + JSON Parsing)
MIT License
353 stars 32 forks source link

Support for error handling on responses with a 200 #41

Closed alladinian closed 7 years ago

alladinian commented 7 years ago

Hi there,

Given that is common for a lot of APIs to return valid JSON responses with (business) errors that are described in the body (alà success = false) with a 200 status code, would it be possible maybe to open functions like handleJSONResponse etc in order to override in a subclass and thus rejecting accordingly? (The point being to treat both system & business errors as reason for rejection in a chain)

Unless I am missing some other way to do that transparently...

Any thoughts?

maxkonovalov commented 7 years ago

Hello @alladinian! What you are looking for is this:

    /**
     Custom error handler block, to parse error returned in response body.
     For example: `{ error: { code: 1, message: "Server error" } }`
     */
    open var errorHandler: ((JSON) -> Error?)? = nil

just set your custom closure on the ws and the call should fail if it returns an Error.

alladinian commented 7 years ago

How did I miss that... @maxkonovalov Thanks man 🍻