Skip to content

Handling Errors with responseDecodable #9

@beninabox

Description

@beninabox

Absolutely great articles, but I have a question.
It seems that the 'happy path' is handled within the project but I'm wondering how I go about handling an error that is returned from the server.

Let's say, for argument's sake, that there was an implementation that retrieves a specific article from the server, and that the user completes an action that causes a request to get an article from the API that doesn't exist. The server might respond with a 404 status code causing the failure case to get executed instead of success. The server returns an error response in the body, but Alamofire still tries to decode the response into the 'success' model, which it inevitably won't be able to do.

I would assume that creating an error response model would be in order here. Something which again conforms to decodable, but how would that be implemented within performRequest function in our APIClient?

There would, of course, be the case where the error cannot be decoded into the error model, in which case we'd have to handle this separately.

Any ideas?
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions