Type casting failed error fixed.

This commit is contained in:
Danish Phiroz 2024-10-16 11:44:50 -04:00
parent 3e1581e28e
commit 9dde688feb

View File

@ -365,11 +365,26 @@ public extension UnkeyedDecodingContainer {
}
// Now get the decoder to use for the type
let decoder = try self.superDecoder()
if let model = try type.init(from: decoder) as? T {
let model = try type.init(from: decoder)
if let model = model as? T {
models.append(model)
} else {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderError: \(typeCodingKey)")
throw ModelRegistry.Error.decoderError
let typeName = "\(T.self)".components(separatedBy: "&").last?.trimmingCharacters(in: .whitespaces) ?? "\(T.self)"
let mirror = Mirror(reflecting: model)
let logMessage = """
ModelRegistry Error:
Model Type Casting Failed:
- Attempted to cast value of type '\(mirror.subjectType)' to '\(typeName)'.
- Molecule Identifier: '\(identifier)'
- Debug Context:
Actual Type: '\(mirror.subjectType)'
Expected Type: '\(typeName)'
"""
MVMCoreLoggingHandler.logDebugMessage(withDelegate: logMessage)
throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: identifier, codingKey: typeCodingKey, codingPath: nestedContainer.codingPath)
}
}