From aa8bc0fae325d9b6243b10dd0f522e1cc24efd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 15 Aug 2018 10:24:28 +0200 Subject: [PATCH] Don't let JSONDecodingRequiredError go JSON decoding: when row doesn't contain any Data, we don't want to expose a JSONDecodingRequiredError error to the user. We switch to a detailed conversion error instead (see #384). --- GRDB/Record/FetchableRecord+Decodable.swift | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index f6fbcef20..135c219eb 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -89,12 +89,11 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer // value here (string, int, double, data, null). If such an // error happens, we'll switch to JSON decoding. return try T(from: RowSingleValueDecoder(row: row, codingPath: codingPath + [key])) - } catch let error as JSONDecodingRequiredError { - if let data = row.dataNoCopy(atIndex: index) { - return try JSONDecoder().decode(type.self, from: data) - } else { - throw error + } catch is JSONDecodingRequiredError { + guard let data = row.dataNoCopy(atIndex: index) else { + fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index)) } + return try JSONDecoder().decode(type.self, from: data) } } } @@ -140,12 +139,11 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer // value here (string, int, double, data, null). If such an // error happens, we'll switch to JSON decoding. return try T(from: RowSingleValueDecoder(row: row, codingPath: codingPath + [key])) - } catch let error as JSONDecodingRequiredError { - if let data = row.dataNoCopy(atIndex: index) { - return try JSONDecoder().decode(type.self, from: data) - } else { - throw error + } catch is JSONDecodingRequiredError { + guard let data = row.dataNoCopy(atIndex: index) else { + fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index)) } + return try JSONDecoder().decode(type.self, from: data) } } }