Skip to content

Commit cf0ebc1

Browse files
author
Itai Ferber
committed
Expose underlying serialization errors
If JSONSerialization or PropertyListSerialization throw errors during encoding or decoding, the error should be exposed as an EncodingError or DecodingError
1 parent f088cfb commit cf0ebc1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

stdlib/public/SDK/Foundation/JSONEncoder.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ open class JSONEncoder {
142142
}
143143

144144
let writingOptions = JSONSerialization.WritingOptions(rawValue: self.outputFormatting.rawValue)
145-
return try JSONSerialization.data(withJSONObject: topLevel, options: writingOptions)
145+
do {
146+
return try JSONSerialization.data(withJSONObject: topLevel, options: writingOptions)
147+
} catch {
148+
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Unable to encode the given top-level value to JSON.", underlyingError: error))
149+
}
146150
}
147151
}
148152

@@ -872,7 +876,13 @@ open class JSONDecoder {
872876
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
873877
/// - throws: An error if any value throws an error during decoding.
874878
open func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {
875-
let topLevel = try JSONSerialization.jsonObject(with: data)
879+
let topLevel: Any
880+
do {
881+
topLevel = try JSONSerialization.jsonObject(with: data)
882+
} catch {
883+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: error))
884+
}
885+
876886
let decoder = _JSONDecoder(referencing: topLevel, options: self.options)
877887
return try T(from: decoder)
878888
}

stdlib/public/SDK/Foundation/PlistEncoder.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ open class PropertyListEncoder {
7474
debugDescription: "Top-level \(Value.self) encoded as date property list fragment."))
7575
}
7676

77-
return try PropertyListSerialization.data(fromPropertyList: topLevel, format: self.outputFormat, options: 0)
77+
do {
78+
return try PropertyListSerialization.data(fromPropertyList: topLevel, format: self.outputFormat, options: 0)
79+
} catch {
80+
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Unable to encode the given top-level value as a property list", underlyingError: error))
81+
}
7882
}
7983
}
8084

@@ -640,7 +644,12 @@ open class PropertyListDecoder {
640644
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.
641645
/// - throws: An error if any value throws an error during decoding.
642646
open func decode<T : Decodable>(_ type: T.Type, from data: Data, format: inout PropertyListSerialization.PropertyListFormat) throws -> T {
643-
let topLevel = try PropertyListSerialization.propertyList(from: data, options: [], format: &format)
647+
let topLevel: Any
648+
do {
649+
topLevel = try PropertyListSerialization.propertyList(from: data, options: [], format: &format)
650+
} catch {
651+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not a valid property list.", underlyingError: error))
652+
}
644653
let decoder = _PlistDecoder(referencing: topLevel, options: self.options)
645654
return try T(from: decoder)
646655
}

0 commit comments

Comments
 (0)