Skip to content

Commit

Permalink
Enable custom types to report errors via ResilientDecodingErrorReport…
Browse files Browse the repository at this point in the history
…er (#35)

* Allow custom types to report errors

* Bump version

Co-authored-by: George <>
  • Loading branch information
GeorgeLyon authored May 21, 2020
1 parent e380c68 commit 1039cd5
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ResilientDecoding.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ResilientDecoding'
s.version = '1.0.2'
s.version = '1.1.0'
s.license = 'MIT'
s.summary = 'A library you can use to partially recover from decoding errors'
s.homepage = 'https://github.com/airbnb/ResilientDecoding'
Expand Down
5 changes: 2 additions & 3 deletions Sources/ResilientDecoding/ErrorReporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ public struct ErrorDigest {
extension Decoder {

/**
This method should be called whenever an error is handled by the `Resilient` infrastructure.
Care should be taken that this is called on the most relevant `Decoder` object, since this method uses the `Decoder`'s `codingPath` to place the error in the correct location in the tree.
Reports an error which did not cause decoding to fail. This error can be accessed after decoding is complete using `ResilientDecodingErrorReporter`. Care should be taken that this is called on the most relevant `Decoder` object, since this method uses the `Decoder`'s `codingPath` to place the error in the correct location in the tree.
*/
func resilientDecodingHandled(_ error: Swift.Error) {
public func reportError(_ error: Swift.Error) {
guard let errorReporterAny = userInfo[.resilientDecodingErrorReporter] else {
return
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ResilientDecoding/Resilient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ extension KeyedDecodingContainer {
}
return try body(decoder)
} catch {
decoder.resilientDecodingHandled(error)
decoder.reportError(error)
return Resilient(fallback(), outcome: .recoveredFrom(error, wasReported: true))
}
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/ResilientDecoding/ResilientArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ extension Decoder {
do {
results.append(.success(transform(try elementDecoder.singleValueContainer().decode(IntermediateElement.self))))
} catch {
elementDecoder.resilientDecodingHandled(error)
elementDecoder.reportError(error)
results.append(.failure(error))
}
}
return Resilient(results)
} catch {
resilientDecodingHandled(error)
reportError(error)
return Resilient([], outcome: .recoveredFrom(error, wasReported: true))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/ResilientDecoding/ResilientDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension Decoder {
.mapValues { $0.result.map(transform) }
return Resilient(value)
} catch {
resilientDecodingHandled(error)
reportError(error)
return Resilient([:], outcome: .recoveredFrom(error, wasReported: true))
}
}
Expand All @@ -73,7 +73,7 @@ private struct DecodingResultContainer<Success: Decodable>: Decodable {
do {
return try decoder.singleValueContainer().decode(Success.self)
} catch {
decoder.resilientDecodingHandled(error)
decoder.reportError(error)
throw error
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ResilientDecoding/ResilientRawRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension KeyedDecodingContainer {
do {
return Resilient(try ResilientRawRepresentableContainer(from: decoder).value).map { $0 }
} catch {
decoder.resilientDecodingHandled(error)
decoder.reportError(error)
return Resilient(T.decodingFallback, outcome: .recoveredFrom(error, wasReported: true))
}
})
Expand Down

0 comments on commit 1039cd5

Please sign in to comment.