Decoder error added for unexpected type

This commit is contained in:
Danish Phiroz 2024-10-17 13:07:19 -04:00
parent 9dde688feb
commit fe12f6855d
2 changed files with 9 additions and 1 deletions

View File

@ -18,6 +18,7 @@ public struct ModelRegistry {
case decoderOther(message: String)
case decoderErrorObjectNotPresent(codingKey: CodingKey, codingPath: [CodingKey])
case decoderErrorModelNotMapped(identifer: String? = nil, codingKey: CodingKey? = nil, codingPath: [CodingKey]? = nil)
case decoderErrorUnexpectedType(actualType: String? = nil, expectedType: String? = nil, identifer: String? = nil, codingKey: CodingKey? = nil, codingPath: [CodingKey]? = nil)
case duplicateRegistration(message: String)
case other(message: String)
case handlerNotMapped(identifer: String? = nil, categoryName: String? = nil)
@ -90,6 +91,10 @@ public struct ModelRegistry {
errorObject.title = "Decoder Error: Model Not Mapped"
let codingPathConcat = codingPath?.compactMap { $0.stringValue }.joined(separator: " ") ?? ""
errorObject.messageToLog = (identifer ?? "") + (codingKey?.stringValue ?? "") + codingPathConcat
case .decoderErrorUnexpectedType(let actualType, let expectedType, let identifer, let codingKey, let codingPath):
errorObject.title = "Decoder Error: Model Type Unexpected"
let codingPathConcat = codingPath?.compactMap { $0.stringValue }.joined(separator: " ") ?? ""
errorObject.messageToLog = "Actual Type: \(actualType ?? "") Expected Type: \(expectedType ?? "")" + (identifer ?? "") + (codingKey?.stringValue ?? "") + codingPathConcat
case .handlerNotMapped(let identifer, let categoryName):
errorObject.title = "Handler Not Mapped"
errorObject.messageToLog = (identifer ?? "") + (categoryName ?? "")
@ -384,7 +389,7 @@ public extension UnkeyedDecodingContainer {
"""
MVMCoreLoggingHandler.logDebugMessage(withDelegate: logMessage)
throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: identifier, codingKey: typeCodingKey, codingPath: nestedContainer.codingPath)
throw ModelRegistry.Error.decoderErrorUnexpectedType(actualType: "\(mirror.subjectType)", expectedType: typeName, identifer: identifier, codingKey: typeCodingKey, codingPath: nestedContainer.codingPath)
}
}

View File

@ -32,6 +32,9 @@ extension ModelRegistry.Error: HumanReadableDecodingErrorProtocol {
case .decoderErrorModelNotMapped(let identifier, let codingKey, let codingPath) where identifier != nil && codingKey != nil && codingPath != nil:
return "Model identifier \"\(identifier!)\" is not mapped for \"\(codingKey!.stringValue)\" @ \(codingPath!.map { return $0.stringValue })"
case .decoderErrorUnexpectedType(let actualType, let expectedType, let identifier, let codingKey, let codingPath) where actualType != nil && expectedType != nil && identifier != nil && codingKey != nil && codingPath != nil:
return "Model identifier \"\(identifier!)\" has unexpected type \"\(actualType!)\" expected type \"\(expectedType!)\" for \"\(codingKey!.stringValue)\" @ \(codingPath!.map { return $0.stringValue })"
case .decoderErrorObjectNotPresent(let codingKey, let codingPath):
return "Required model \"\(codingKey.stringValue)\" was not found @ \(codingPath.map { return $0.stringValue })"