URLSesssion and Error Handling

I have seen the URL Session tutoriala, but all of them just skip over handling errors, does anyone have a good resource for actually being able to return information to the user when there is an issue? For instance, 401 returns account does not exist.

Hi @mike.foote,
What you are after is the HTTP server Status Codes, these are standard from any server implementation, you can get the list of all the codes on couple of sites. Here is one that I can refer you to

Cheers,

@jayantvarma, I understand what HTTP codes are, but with all codes come a message, I would like to see a swift 4/xcode way to parse those, example, in PHP you would use getMessage() from the httpResponse. I don’t want to just dump out and not tell the EU they are using the wrong email or wrong password, I want to be able to notify the EU of what the issue is.

Hi @mike.foote,
Ok, in the NSURLSession when you get the data there are three parameters in the closure,
Data, URLResponse and Error. So when you check for the status code you get that from the URLResponse and from the error you can get the message and details if you want.

Cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.