From e43a32baec80ebbcfed4d0330d0be50eb73434c7 Mon Sep 17 00:00:00 2001 From: Susan Cheng Date: Sat, 2 May 2020 03:30:45 +0800 Subject: [PATCH] Fix force unwrap in PostgresDataDecoder (#184) * Replace the force unwrap to throws Things crash in here * missing return --- Sources/PostgresKit/PostgresDataDecoder.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/PostgresKit/PostgresDataDecoder.swift b/Sources/PostgresKit/PostgresDataDecoder.swift index 774c9f2d..11ccfdc0 100644 --- a/Sources/PostgresKit/PostgresDataDecoder.swift +++ b/Sources/PostgresKit/PostgresDataDecoder.swift @@ -139,7 +139,13 @@ 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)" + )) + } + return value as! T } else { return try T.init(from: _Decoder(data: self.data, json: self.json)) }