Skip to content

Commit bf06e4b

Browse files
committed
feat: Logging decoding errors
1 parent 5cf2c46 commit bf06e4b

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// DecodingErrorFormatter.swift
3+
// GoodNetworking
4+
//
5+
// Created by Filip Šašala on 29/09/2025.
6+
//
7+
8+
import Foundation
9+
10+
internal extension DecodingError {
11+
12+
var prettyPrinted: String {
13+
switch self {
14+
case .typeMismatch(_, let context),
15+
.valueNotFound(_, let context),
16+
.keyNotFound(_, let context),
17+
.dataCorrupted(let context):
18+
"""
19+
⛔️ Decoding failed - \(context.debugDescription)
20+
Coding path: \(context.codingPath.prettyPrinted)
21+
"""
22+
23+
@unknown default:
24+
"⛔️ Decoding failed - unknown error"
25+
}
26+
}
27+
28+
}
29+
30+
private extension Array where Element == CodingKey {
31+
32+
var prettyPrinted: String {
33+
map { $0.prettyPrinted }.joined(separator: "")
34+
}
35+
36+
}
37+
38+
private extension CodingKey {
39+
40+
var prettyPrinted: String {
41+
if let intValue = intValue {
42+
return "[\(intValue)]"
43+
}
44+
return stringValue
45+
}
46+
47+
}

Sources/GoodNetworking/Session/NetworkSession.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ extension NetworkSession {
227227
let model = try decoder.decode(T.self, from: data)
228228
return model
229229
} catch let error as DecodingError {
230+
logger.logNetworkEvent(
231+
message: error.prettyPrinted,
232+
level: .error,
233+
file: #file,
234+
line: #line
235+
)
230236
throw error.asNetworkError()
231237
} catch {
232238
throw URLError(.cannotDecodeRawData).asNetworkError()

0 commit comments

Comments
 (0)