Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more information about the decoding errors #2

Closed
mdb1 opened this issue May 23, 2023 · 0 comments · Fixed by #3
Closed

Add more information about the decoding errors #2

mdb1 opened this issue May 23, 2023 · 0 comments · Fixed by #3

Comments

@mdb1
Copy link
Owner

mdb1 commented May 23, 2023

@gentilijuanmanuel I found the piece of code I was looking for:

import Foundation

// sourcery: AutoMockable
// Represents object that is able to log decoding errors in details
protocol DecodingErrorLogging: AnyObject {
    /// This method is used to debug decoding failures
    /// - Parameters:
    ///   - error: Error to log information on
    ///   - type: Type that decoding was attempted for
    func logAdditionalDecodingFailureInfo(with error: Error, for type: Decodable.Type)
}

/// Class that logs error on decoding network models
final class DecodingErrorLogger: DecodingErrorLogging {
    private let jsonDecoder: JSONDecoding
    private let loggerService: Logging

    init(
        jsonDecoder: JSONDecoding = BFJSONDecoder(keyDecodingStrategy: .convertFromSnakeCase),
        loggerService: Logging = NetworkingServiceLocator.logger
    ) {
        self.jsonDecoder = jsonDecoder
        self.loggerService = loggerService
    }

    func logAdditionalDecodingFailureInfo(with error: Error, for type: Decodable.Type) {
        var errorDescription: String?
        var logProperties: [LoggerPropertyKeys: String] = [:]

        if let decodingError = error as? DecodingError {
            switch decodingError {
            case let .dataCorrupted(context):
                // An indication that the data is corrupted or otherwise invalid.
                addContext(context, logProperties: &logProperties)
                errorDescription = LoggerPropertyKeys.dataCorrupted.rawValue
            case let .keyNotFound(key, context):
                // An indication that a keyed decoding container was asked for an entry for the given key,
                // but did not contain one.
                addContext(context, logProperties: &logProperties)
                errorDescription = "Key '\(key)' not found"
            case let .valueNotFound(value, context):
                // An indication that a non-optional value of the given type was expected, but a null value was found.
                addContext(context, logProperties: &logProperties)
                errorDescription = "Value '\(value)' not found"
            case let .typeMismatch(type, context):
                // An indication that a value of the given type could not be decoded because
                // it did not match the type of what was found in the encoded payload.
                addContext(context, logProperties: &logProperties)
                errorDescription = "Type '\(type)' mismatch"
            default: ()
            }
        }
        logProperties[.decodingError] = errorDescription ?? error.localizedDescription
        logProperties[.model] = "\(type)"

        // Log as warning so the information reaches AppCenter Crash reports
        loggerService.log(.warn, .decodingFailure, properties: logProperties)
    }
}

private extension DecodingErrorLogger {
    /// Add Decoding Error context information to the dictionary
    func addContext(
        _ context: DecodingError.Context,
        logProperties: inout [LoggerPropertyKeys: String]
    ) {
        logProperties[.contextProperty] = context.debugDescription
        logProperties[.codingPathProperty] = context.codingPath.debugDescription
        logProperties[.underlyingErrorProperty] = context.underlyingError.debugDescription
    }
}

Is something like that, basically, it prints more information about the particular error in the decoding phase.

Maybe we can add something similar to this to this package 😄

@mdb1 mdb1 changed the title Add Add more information about the decoding errors May 23, 2023
@mdb1 mdb1 closed this as completed in #3 May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant