DEV Community

Liang Wang
Liang Wang

Posted on

Decoding Error

A great read to show better decoding error!
Link: [https://stackoverflow.com/a/53231548]

When debugging, never use localizedDescription in Decodable catch blocks.
In the simple form just print(error)
It shows the full error including the crucial information debugDescription and context.Decodable errors are very comprehensive.

                                                                      While developing code you could catch each Decodable error separately, for example
Enter fullscreen mode Exit fullscreen mode
} catch let DecodingError.dataCorrupted(context) {
  print(context)
} catch let DecodingError.keyNotFound(key, context) {
  print("Key '\(key)' not found:", context.debugDescription)
  print("codingPath:", context.codingPath)
} catch let DecodingError.valueNotFound(value, context) {
  print("Value '\(value)' not found:", context.debugDescription)
  print("codingPath:", context.codingPath)
} catch let DecodingError.typeMismatch(type, context)  {
  print("Type '\(type)' mismatch:", context.debugDescription)
  print("codingPath:", context.codingPath)
} catch {
  print("error: ", error)
}

Enter fullscreen mode Exit fullscreen mode

It shows only the most significant information.

Top comments (0)