Skip to content

Commit dd1b740

Browse files
authored
Add PostgresDecodingError debugDescription (#358)
1 parent dbf9c2e commit dd1b740

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

Sources/PostgresNIO/New/PSQLError.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public struct PSQLError: Error {
393393

394394
/// An error that may happen when a ``PostgresRow`` or ``PostgresCell`` is decoded to native Swift types.
395395
public struct PostgresDecodingError: Error, Equatable {
396-
public struct Code: Hashable, Error {
396+
public struct Code: Hashable, Error, CustomStringConvertible {
397397
enum Base {
398398
case missingData
399399
case typeMismatch
@@ -409,6 +409,17 @@ public struct PostgresDecodingError: Error, Equatable {
409409
public static let missingData = Self.init(.missingData)
410410
public static let typeMismatch = Self.init(.typeMismatch)
411411
public static let failure = Self.init(.failure)
412+
413+
public var description: String {
414+
switch self.base {
415+
case .missingData:
416+
return "missingData"
417+
case .typeMismatch:
418+
return "typeMismatch"
419+
case .failure:
420+
return "failure"
421+
}
422+
}
412423
}
413424

414425
/// The decoding error code
@@ -476,3 +487,24 @@ extension PostgresDecodingError: CustomStringConvertible {
476487
"Database error"
477488
}
478489
}
490+
491+
extension PostgresDecodingError: CustomDebugStringConvertible {
492+
public var debugDescription: String {
493+
var result = #"PostgresDecodingError(code: \#(self.code)"#
494+
495+
result.append(#", columnName: \#(String(reflecting: self.columnName))"#)
496+
result.append(#", columnIndex: \#(self.columnIndex)"#)
497+
result.append(#", targetType: \#(String(reflecting: self.targetType))"#)
498+
result.append(#", postgresType: \#(self.postgresType)"#)
499+
result.append(#", postgresFormat: \#(self.postgresFormat)"#)
500+
if let postgresData = self.postgresData {
501+
result.append(#", postgresData: \#(postgresData.debugDescription)"#) // https://github.com/apple/swift-nio/pull/2418
502+
}
503+
result.append(#", file: \#(self.file)"#)
504+
result.append(#", line: \#(self.line)"#)
505+
result.append(")")
506+
507+
return result
508+
}
509+
}
510+

Tests/PostgresNIOTests/New/PostgresErrorTests.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class PostgresDecodingErrorTests: XCTestCase {
3434
}
3535

3636
func testPostgresDecodingErrorDescription() {
37-
let error = PostgresDecodingError(
37+
let error1 = PostgresDecodingError(
3838
code: .typeMismatch,
3939
columnName: "column",
4040
columnIndex: 0,
@@ -46,6 +46,39 @@ final class PostgresDecodingErrorTests: XCTestCase {
4646
line: 123
4747
)
4848

49-
XCTAssertEqual("\(error)", "Database error")
49+
let error2 = PostgresDecodingError(
50+
code: .missingData,
51+
columnName: "column",
52+
columnIndex: 0,
53+
targetType: [[String: String]].self,
54+
postgresType: .jsonbArray,
55+
postgresFormat: .binary,
56+
postgresData: nil,
57+
file: "bar.swift",
58+
line: 123
59+
)
60+
61+
// Plain description
62+
XCTAssertEqual(String(describing: error1), "Database error")
63+
XCTAssertEqual(String(describing: error2), "Database error")
64+
65+
// Extended debugDescription
66+
XCTAssertEqual(String(reflecting: error1), """
67+
PostgresDecodingError(code: typeMismatch,\
68+
columnName: "column", columnIndex: 0,\
69+
targetType: Swift.String,\
70+
postgresType: TEXT, postgresFormat: binary,\
71+
postgresData: \(error1.postgresData?.debugDescription ?? "nil"),\
72+
file: foo.swift, line: 123\
73+
)
74+
""")
75+
XCTAssertEqual(String(reflecting: error2), """
76+
PostgresDecodingError(code: missingData,\
77+
columnName: "column", columnIndex: 0,\
78+
targetType: Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>,\
79+
postgresType: JSONB[], postgresFormat: binary,\
80+
file: bar.swift, line: 123\
81+
)
82+
""")
5083
}
5184
}

0 commit comments

Comments
 (0)