You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import Foundation
// sourcery: AutoMockable
// Represents object that is able to log decoding errors in details
protocolDecodingErrorLogging: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
finalclassDecodingErrorLogger:DecodingErrorLogging{privateletjsonDecoder:JSONDecodingprivateletloggerService:Logginginit(
jsonDecoder:JSONDecoding=BFJSONDecoder(keyDecodingStrategy:.convertFromSnakeCase),
loggerService:Logging=NetworkingServiceLocator.logger
){self.jsonDecoder = jsonDecoder
self.loggerService = loggerService
}func logAdditionalDecodingFailureInfo(with error:Error, for type:Decodable.Type){varerrorDescription:String?varlogProperties:[LoggerPropertyKeys:String]=[:]iflet decodingError = error as?DecodingError{switch decodingError {caselet.dataCorrupted(context):
// An indication that the data is corrupted or otherwise invalid.
addContext(context, logProperties:&logProperties)
errorDescription =LoggerPropertyKeys.dataCorrupted.rawValue
caselet.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"caselet.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"caselet.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)}}privateextensionDecodingErrorLogger{
/// 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 😄
The text was updated successfully, but these errors were encountered:
mdb1
changed the title
Add
Add more information about the decoding errors
May 23, 2023
@gentilijuanmanuel I found the piece of code I was looking for:
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 😄
The text was updated successfully, but these errors were encountered: