Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
improve logging of Decoder errors (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
theffc authored Jun 26, 2020
1 parent f6787dc commit 42c4390
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions iOS/Schema/Sources/Decoding/AnyDecodableContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ extension AnyDecodableContainer: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

if let type = try? container.decode(String.self, forKey: .componentType) {
if let decodable = dependencies.decoder.componentType(forType: type.lowercased()) {
content = try decodable.init(from: decoder)
if let decodable = dependencies.decoder.componentType(forType: type.lowercased()) {
content = try decodable.init(from: decoder)
} else {
dependencies.schemaLogger?.logDecodingError(type: type)
content = UnknownComponent(type: type)
Expand Down
18 changes: 15 additions & 3 deletions iOS/Schema/Sources/Decoding/ComponentDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,27 @@ final public class ComponentDecoder: ComponentDecoding {
}

public func decodeComponent(from data: Data) throws -> RawComponent {
return try decode(from: data)
return try decodeAndLog(from: data)
}

public func decodeAction(from data: Data) throws -> RawAction {
return try decode(from: data)
return try decodeAndLog(from: data)
}

// MARK: - Private Functions


private func decodeAndLog<T>(from data: Data) throws -> T {
do {
return try decode(from: data)
} catch let error as DecodingError {
dependencies.schemaLogger?.logDecodingError(type: String(describing: error))
throw error
} catch {
dependencies.schemaLogger?.logDecodingError(type: error.localizedDescription)
throw error
}
}

private func decode<T>(from data: Data) throws -> T {
let container = try jsonDecoder.decode(AnyDecodableContainer.self, from: data)
guard let content = container.content as? T else {
Expand Down

0 comments on commit 42c4390

Please sign in to comment.