Skip to content

Commit 873b3b7

Browse files
Allow critical event log to be exported with mixed 3.2.x and 3.3 formats (LoopKit#532)
* allow critical event log to be exported with mixed 3.2.x and 3.3 formats * Added printout of value to the log --------- Co-authored-by: Jonas Björkert <[email protected]>
1 parent 2f535b3 commit 873b3b7

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

LoopKit/JSONStreamEncoder.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ public class JSONStreamEncoder {
4545
}
4646

4747
for value in values {
48-
try stream.write(encoded ? ",\n" : "[\n")
49-
try stream.write(try Self.encoder.encode(value))
50-
encoded = true
48+
do {
49+
let encodedData = try Self.encoder.encode(value)
50+
try stream.write(encoded ? ",\n" : "[\n")
51+
try stream.write(encodedData)
52+
encoded = true
53+
} catch {
54+
// Log the error and insert a placeholder or error message in the JSON
55+
print("Failed to encode value: \(value) with error: \(error.localizedDescription)")
56+
let errorInfo = "{\"error\": \"Failed to encode value: \(value) due to: \(error.localizedDescription)\"}"
57+
try stream.write(encoded ? ",\n" : "[\n")
58+
try stream.write(errorInfo.data(using: .utf8)!)
59+
encoded = true
60+
}
5161
}
5262
}
5363

0 commit comments

Comments
 (0)