--- a/TalerWallet1/Views/Main/ErrorView.swift +++ b/TalerWallet1/Views/Main/ErrorView.swift @@ -253,11 +253,38 @@ case .wrongParameters: self.init(stack, title: wrongParameters, copyable: false, onDismiss: onDismiss) } + case let decodingError as DecodingError: + // An answer this app could not read. Foundation's sentence for it ("The data + // couldn't be read because it is missing.") names nothing; say what was missing + // and let the user copy it - it is all a bug report can go by. + self.init(stack, title: deserializationError, + message: Self.describe(decodingError), + copyable: true, onDismiss: onDismiss) default: self.init(stack, title: unknownError, message: error.localizedDescription, copyable: false, onDismiss: onDismiss) } } + /// What a `DecodingError` is about: the key or type, and where in the answer. + static func describe(_ error: DecodingError) -> String { + func path(_ context: DecodingError.Context) -> String { + let p = context.codingPath.map { $0.stringValue }.joined(separator: ".") + return p.isEmpty ? "" : " at '\(p)'" + } + switch error { + case .keyNotFound(let key, let context): + return "Key '\(key.stringValue)' not found" + path(context) + case .valueNotFound(let type, let context): + return "No value of type \(type)" + path(context) + case .typeMismatch(let type, let context): + return "Expected \(type)" + path(context) + case .dataCorrupted(let context): + return context.debugDescription + path(context) + @unknown default: + return error.localizedDescription + } + } + init(_ stack: CallStack?, data: ErrorData, devMode: Bool, onDismiss: (() -> Void)? = nil) { switch data { case .message(let title, let message):