0

I am trying to display the error message to user as follows with error.localizedDescription. Should I trust error message; if not, what could be used instead?

 URLSession.shared.dataTask(with: request) { data, response, error in
  if let error = error {
    DispatchQueue.main.async {
      self.showAlert(message: error.localizedDescription)
    }
    return
  }

1 Answer 1

2

Just showing an error to the user that you don't know what means is definitely not a good idea.

Imagine you are using an app and when you try to refresh or perform an action you get a message like "Error: BAD GATEWAY 24231". This information can be useful for you as a developer but not so much for the user.

You should always aim to show an error message that the user can act on. For example, would be much useful if you just do:

self.showAlert(message: "Operation Failed. Please try again.")

In this case the user has something to do other than wondering what "BAD GATEWAY" means (and even if they know, is unlikely they can do something about it).

That said, it really depends on the domain of your app. You might want to check for specific errors and perform different actions or show different messages. For example, if the error is about an item not being found, you can display an empty view to the user or even telling them "The item you are looking for does not exists." But displaying an error that you don't really control is more likely a bad idea (unless in development or beta testing and you have instructed your users to send you the error, but even in those cases there are better mechanisms).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.