Printing DecodingError details on decode failed in Swift
catch let error as DecodingError {
switch error {
case .typeMismatch(let key, let value):
print("error \(key), value \(value) and ERROR: \(error.localizedDescription)")
case .valueNotFound(let key, let value):
print("error \(key), value \(value) and ERROR: \(error.localizedDescription)")
case .keyNotFound(let key, let value):
print("error \(key), value \(value) and ERROR: \(error.localizedDescription)")
case .dataCorrupted(let key):
print("error \(key), and ERROR: \(error.localizedDescription)")
default:
print("ERROR: \(error.localizedDescription)")
}
}
DecodingError
is an enum. In your case you have to catch
the typeMismatch
case and print the type
and the context
.
catch let DecodingError.typeMismatch(type, context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
}