Expose underlying serialization errors
If JSONSerialization or PropertyListSerialization throw errors during encoding or decoding, the error should be exposed as an EncodingError or DecodingError
diff --git a/stdlib/public/SDK/Foundation/JSONEncoder.swift b/stdlib/public/SDK/Foundation/JSONEncoder.swift
index 1751ae8..49d2ec4 100644
--- a/stdlib/public/SDK/Foundation/JSONEncoder.swift
+++ b/stdlib/public/SDK/Foundation/JSONEncoder.swift
@@ -142,7 +142,11 @@
}
let writingOptions = JSONSerialization.WritingOptions(rawValue: self.outputFormatting.rawValue)
- return try JSONSerialization.data(withJSONObject: topLevel, options: writingOptions)
+ do {
+ return try JSONSerialization.data(withJSONObject: topLevel, options: writingOptions)
+ } catch {
+ throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Unable to encode the given top-level value to JSON.", underlyingError: error))
+ }
}
}
@@ -872,7 +876,13 @@
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
/// - throws: An error if any value throws an error during decoding.
open func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {
- let topLevel = try JSONSerialization.jsonObject(with: data)
+ let topLevel: Any
+ do {
+ topLevel = try JSONSerialization.jsonObject(with: data)
+ } catch {
+ throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: error))
+ }
+
let decoder = _JSONDecoder(referencing: topLevel, options: self.options)
return try T(from: decoder)
}
diff --git a/stdlib/public/SDK/Foundation/PlistEncoder.swift b/stdlib/public/SDK/Foundation/PlistEncoder.swift
index df898d2..f5514ff 100644
--- a/stdlib/public/SDK/Foundation/PlistEncoder.swift
+++ b/stdlib/public/SDK/Foundation/PlistEncoder.swift
@@ -74,7 +74,11 @@
debugDescription: "Top-level \(Value.self) encoded as date property list fragment."))
}
- return try PropertyListSerialization.data(fromPropertyList: topLevel, format: self.outputFormat, options: 0)
+ do {
+ return try PropertyListSerialization.data(fromPropertyList: topLevel, format: self.outputFormat, options: 0)
+ } catch {
+ throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Unable to encode the given top-level value as a property list", underlyingError: error))
+ }
}
}
@@ -640,7 +644,12 @@
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.
/// - throws: An error if any value throws an error during decoding.
open func decode<T : Decodable>(_ type: T.Type, from data: Data, format: inout PropertyListSerialization.PropertyListFormat) throws -> T {
- let topLevel = try PropertyListSerialization.propertyList(from: data, options: [], format: &format)
+ let topLevel: Any
+ do {
+ topLevel = try PropertyListSerialization.propertyList(from: data, options: [], format: &format)
+ } catch {
+ throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not a valid property list.", underlyingError: error))
+ }
let decoder = _PlistDecoder(referencing: topLevel, options: self.options)
return try T(from: decoder)
}