Skip to content

Commit f088cfb

Browse files
author
Itai Ferber
committed
Add DecodingError.dataCorrupted conveniences
DecodingError.dataCorrupted is an error most commonly thrown from user code. We can expose conveniences to more easily throw .dataCorrupted errors.
1 parent 92fccf9 commit f088cfb

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

stdlib/public/core/Codable.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,10 @@ public protocol UnkeyedDecodingContainer {
17401740
/// A container that can support the storage and direct encoding of a single
17411741
/// non-keyed value.
17421742
public protocol SingleValueEncodingContainer {
1743+
/// The path of coding keys taken to get to this point in encoding.
1744+
/// A `nil` value indicates an unkeyed container.
1745+
var codingPath: [CodingKey?] { get }
1746+
17431747
/// Encodes a null value.
17441748
///
17451749
/// - throws: `EncodingError.invalidValue` if a null value is invalid in the current context for this format.
@@ -1854,6 +1858,10 @@ public protocol SingleValueEncodingContainer {
18541858

18551859
/// A `SingleValueDecodingContainer` is a container which can support the storage and direct decoding of a single non-keyed value.
18561860
public protocol SingleValueDecodingContainer {
1861+
/// The path of coding keys taken to get to this point in encoding.
1862+
/// A `nil` value indicates an unkeyed container.
1863+
var codingPath: [CodingKey?] { get }
1864+
18571865
/// Decodes a null value.
18581866
///
18591867
/// - returns: Whether the encountered value was null.
@@ -2175,6 +2183,47 @@ public enum DecodingError : Error {
21752183
}
21762184
}
21772185

2186+
// The following extensions allow for easier error construction.
2187+
2188+
public extension DecodingError {
2189+
/// A convenience method which creates a new .dataCorrupted error using a constructed coding path and the given debug description.
2190+
///
2191+
/// Constructs a coding path by appending the given key to the given container's coding path.
2192+
///
2193+
/// - param key: The key which caused the failure.
2194+
/// - param container: The container in which the corrupted data was accessed.
2195+
/// - param debugDescription: A description of the error to aid in debugging.
2196+
static func dataCorruptedError<C : KeyedDecodingContainerProtocol>(forKey key: C.Key, in container: C, debugDescription: String) -> DecodingError {
2197+
let context = DecodingError.Context(codingPath: container.codingPath + [key],
2198+
debugDescription: debugDescription)
2199+
return .dataCorrupted(context)
2200+
}
2201+
2202+
/// A convenience method which creates a new .dataCorrupted error using a constructed coding path and the given debug description.
2203+
///
2204+
/// Constructs a coding path by appending a nil key to the given container's coding path.
2205+
///
2206+
/// - param container: The container in which the corrupted data was accessed.
2207+
/// - param debugDescription: A description of the error to aid in debugging.
2208+
static func dataCorruptedError(in container: UnkeyedDecodingContainer, debugDescription: String) -> DecodingError {
2209+
let context = DecodingError.Context(codingPath: container.codingPath + [nil],
2210+
debugDescription: debugDescription)
2211+
return .dataCorrupted(context)
2212+
}
2213+
2214+
/// A convenience method which creates a new .dataCorrupted error using a constructed coding path and the given debug description.
2215+
///
2216+
/// Uses the given container's coding path as the constructed path.
2217+
///
2218+
/// - param container: The container in which the corrupted data was accessed.
2219+
/// - param debugDescription: A description of the error to aid in debugging.
2220+
static func dataCorruptedError(in container: SingleValueDecodingContainer, debugDescription: String) -> DecodingError {
2221+
let context = DecodingError.Context(codingPath: container.codingPath,
2222+
debugDescription: debugDescription)
2223+
return .dataCorrupted(context)
2224+
}
2225+
}
2226+
21782227
//===----------------------------------------------------------------------===//
21792228
// Keyed Encoding Container Implementations
21802229
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)