Skip to content

Instantly share code, notes, and snippets.

@emin-grbo
Created February 13, 2024 10:39
Show Gist options
  • Save emin-grbo/030e4b0de13ba05d3900a51146c8203e to your computer and use it in GitHub Desktop.
Save emin-grbo/030e4b0de13ba05d3900a51146c8203e to your computer and use it in GitHub Desktop.
DecodeOrReport
// Used to detect specific issue with JSON decoding
func decodeOrReport(data: Data) {
do {
let _ = try JSONDecoder().decode(WidgetResponse.self, from: data)
print("\n\n👍ALL GOOD\n\n")
} catch DecodingError.keyNotFound( let key, let context) {
print("\n\n⛔️FAILED TO DECODE\n\n")
print("could not find key \(key) in JSON: \(context.debugDescription)")
} catch DecodingError.valueNotFound( let type, let context) {
print("\n\n⛔️FAILED TO DECODE\n\n")
print("could not find type \(type) in JSON: \(context.debugDescription)")
} catch DecodingError.typeMismatch( let type, let context) {
print("\n\n⛔️FAILED TO DECODE\n\n")
print("type mismatch for type \(type) in JSON: \(context.debugDescription)")
} catch DecodingError.dataCorrupted( let context) {
print("\n\n⛔️FAILED TO DECODE\n\n")
print("data found to be corrupted in JSON: \(context.debugDescription)")
} catch let error as NSError {
print("\n\n⛔️FAILED TO DECODE\n\n")
print(String(data: data, encoding: String.Encoding.utf8) ?? "NIL")
print("\n\n\n —————————— ")
NSLog("Error in read(from:ofType:) domain = \(error.domain), description= \(error.localizedDescription)")
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment