Skip to content

Commit d01d94d

Browse files
authored
Merge pull request #53 from GoodRequest/log-decoding-errors
Log decoding errors
2 parents 5cf2c46 + cf7116d commit d01d94d

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

Package.resolved

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
return "⛔️ Error while decoding \(context.codingPath.prettyPrinted) - \(context.debugDescription)"
19+
20+
@unknown default:
21+
return "⛔️ Decoding failed - unknown error"
22+
}
23+
}
24+
25+
}
26+
27+
private extension Array where Element == CodingKey {
28+
29+
var prettyPrinted: String {
30+
map { $0.prettyPrinted }.joined(separator: "")
31+
}
32+
33+
}
34+
35+
private extension CodingKey {
36+
37+
var prettyPrinted: String {
38+
if let intValue = intValue {
39+
return "[\(intValue)]"
40+
}
41+
return stringValue
42+
}
43+
44+
}

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)