From eb6fac353de6320f842ffa769610054a5cfee14a Mon Sep 17 00:00:00 2001 From: Susan Cheng Date: Wed, 29 Apr 2020 22:48:46 +0800 Subject: [PATCH 1/2] Replace the force unwrap to throws Things crash in here --- Sources/PostgresKit/PostgresDataDecoder.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/PostgresKit/PostgresDataDecoder.swift b/Sources/PostgresKit/PostgresDataDecoder.swift index 774c9f2d..b8edd681 100644 --- a/Sources/PostgresKit/PostgresDataDecoder.swift +++ b/Sources/PostgresKit/PostgresDataDecoder.swift @@ -139,7 +139,12 @@ public final class PostgresDataDecoder { func decode(_ type: T.Type) throws -> T where T : Decodable { if let convertible = T.self as? PostgresDataConvertible.Type { - return convertible.init(postgresData: self.data)! as! T + guard let value = convertible.init(postgresData: data) else { + throw DecodingError.typeMismatch(T.self, DecodingError.Context.init( + codingPath: [], + debugDescription: "Could not convert to \(T.self): \(data)" + )) + } } else { return try T.init(from: _Decoder(data: self.data, json: self.json)) } From cd668ea5ef967c8eba12ad970e026abb62945540 Mon Sep 17 00:00:00 2001 From: Susan Cheng Date: Wed, 29 Apr 2020 23:33:18 +0800 Subject: [PATCH 2/2] missing return --- Sources/PostgresKit/PostgresDataDecoder.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/PostgresKit/PostgresDataDecoder.swift b/Sources/PostgresKit/PostgresDataDecoder.swift index b8edd681..11ccfdc0 100644 --- a/Sources/PostgresKit/PostgresDataDecoder.swift +++ b/Sources/PostgresKit/PostgresDataDecoder.swift @@ -145,6 +145,7 @@ public final class PostgresDataDecoder { debugDescription: "Could not convert to \(T.self): \(data)" )) } + return value as! T } else { return try T.init(from: _Decoder(data: self.data, json: self.json)) }