Skip to content

Commit e37d38d

Browse files
authored
Improved Encoding/Decoding error descriptions (#28)
Improved Encoding/Decoding error descriptions ### Motivation When an EncodingError/DecodingError is thrown and printed, we used to omit the the coding path and underlying error, which are often crucial in finding e.g. which part of the payload has a missing field. ### Modifications This PR improves the logging to include the coding path and underlying error in the output. ### Result Now when e.g. a DecodingError is thrown and printed, the default output has enough information to know which part of the JSON payload is malformed. ### Test Plan Tested manually, as we don't have unit tests for exact printing strings. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (api breakage) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #28
1 parent c5ab4e8 commit e37d38d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Sources/OpenAPIRuntime/Errors/CodingErrors.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,43 @@ extension DecodingError: PrettyStringConvertible {
1818
let output: String
1919
switch self {
2020
case .dataCorrupted(let context):
21-
output = "dataCorrupted - \(context.debugDescription)"
21+
output = "dataCorrupted - \(context.prettyDescription)"
2222
case .keyNotFound(let key, let context):
23-
output = "keyNotFound \(key) - \(context.debugDescription)"
23+
output = "keyNotFound \(key) - \(context.prettyDescription)"
2424
case .typeMismatch(let type, let context):
25-
output = "typeMismatch \(type) - in \(context.debugDescription)"
25+
output = "typeMismatch \(type) - \(context.prettyDescription)"
2626
case .valueNotFound(let type, let context):
27-
output = "valueNotFound \(type) - \(context.debugDescription)"
27+
output = "valueNotFound \(type) - \(context.prettyDescription)"
2828
@unknown default:
2929
output = "unknown: \(localizedDescription)"
3030
}
3131
return "DecodingError: \(output)"
3232
}
3333
}
3434

35+
extension DecodingError.Context: PrettyStringConvertible {
36+
var prettyDescription: String {
37+
let path = codingPath.map(\.description).joined(separator: "/")
38+
return "at \(path): \(debugDescription) (underlying error: \(underlyingError?.localizedDescription ?? "<nil>"))"
39+
}
40+
}
41+
3542
extension EncodingError: PrettyStringConvertible {
3643
var prettyDescription: String {
3744
let output: String
3845
switch self {
3946
case .invalidValue(let value, let context):
40-
output = "invalidValue \(value) - \(context)"
47+
output = "invalidValue \(value) - \(context.prettyDescription)"
4148
@unknown default:
4249
output = "unknown: \(localizedDescription)"
4350
}
4451
return "EncodingError: \(output)"
4552
}
4653
}
54+
55+
extension EncodingError.Context: PrettyStringConvertible {
56+
var prettyDescription: String {
57+
let path = codingPath.map(\.description).joined(separator: "/")
58+
return "at \(path): \(debugDescription) (underlying error: \(underlyingError?.localizedDescription ?? "<nil>"))"
59+
}
60+
}

0 commit comments

Comments
 (0)