From 214cb75b985f757ac7fa6916da916f82abac3142 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Wed, 24 Jun 2020 15:36:43 -0400 Subject: [PATCH] attach coding key data to ModelRegistry errors. --- .../MVMCore/Models/Model/ModelRegistry.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index f9ec58b..93407d1 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -16,8 +16,8 @@ public struct ModelRegistry { case encoderError case decoderError case decoderOther(message: String) - case decoderErrorObjectNotPresent - case decoderErrorModelNotMapped + case decoderErrorObjectNotPresent(codingKey: CodingKey, codingPath: [CodingKey]) + case decoderErrorModelNotMapped(identifer: String? = nil, codingKey: CodingKey? = nil, codingPath: [CodingKey]? = nil) case other(message: String) } @@ -75,8 +75,8 @@ extension KeyedDecodingContainer where Key: CodingKey { /// Decodes to a registered model based on the identifier public func decodeModel(codingKey: KeyedDecodingContainer.Key) throws -> T { guard let model: T = try decodeModelIfPresent(codingKey: codingKey) else { - MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey)") - throw ModelRegistry.Error.decoderErrorObjectNotPresent + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey.stringValue)") + throw ModelRegistry.Error.decoderErrorObjectNotPresent(codingKey: codingKey, codingPath: codingPath) } return model } @@ -84,8 +84,8 @@ extension KeyedDecodingContainer where Key: CodingKey { /// Decodes an array of registered model based on the identifiers. public func decodeModels(codingKey: KeyedDecodingContainer.Key) throws -> [T] { guard let model: [T] = try decodeModelsIfPresent(codingKey: codingKey) else { - MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey)") - throw ModelRegistry.Error.decoderErrorObjectNotPresent + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey.stringValue)") + throw ModelRegistry.Error.decoderErrorObjectNotPresent(codingKey: codingKey, codingPath: codingPath) } return model } @@ -93,8 +93,8 @@ extension KeyedDecodingContainer where Key: CodingKey { /// Decodes an array with arrays of models based on the identifiers. public func decodeModels2D(codingKey: KeyedDecodingContainer.Key) throws -> [[T]] { guard let models: [[T]] = try decodeModels2DIfPresent(codingKey: codingKey) else { - MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey)") - throw ModelRegistry.Error.decoderErrorObjectNotPresent + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey.stringValue)") + throw ModelRegistry.Error.decoderErrorObjectNotPresent(codingKey: codingKey, codingPath: codingPath) } return models } @@ -114,7 +114,7 @@ extension KeyedDecodingContainer where Key: CodingKey { //get the type from the identifier value in the Registry guard let type = ModelRegistry.getType(for: identifier, with: T.self) else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelProtocol not mapped: \(identifier)") - throw ModelRegistry.Error.decoderErrorModelNotMapped + throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: identifier, codingKey: typeCodingKey, codingPath: container.codingPath) } //decode the type using the decoder @@ -208,7 +208,7 @@ public extension UnkeyedDecodingContainer { let identifier = try nestedContainer.decode(String.self, forKey: typeCodingKey) guard let type = ModelRegistry.getType(for: identifier, with: T.self) else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorModelNotMapped: \(identifier)") - throw ModelRegistry.Error.decoderErrorModelNotMapped + throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: identifier, codingKey: typeCodingKey, codingPath: nestedContainer.codingPath) } // Now get the decoder to use for the type let decoder = try self.superDecoder() @@ -233,7 +233,7 @@ public extension UnkeyedDecodingContainer { var arraycontainerCopy = try containerCopy.nestedUnkeyedContainer() guard let models: [T] = try arraycontainerCopy.decodeModelsIfPresent() else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorModelNotMapped: \(typeCodingKey)") - throw ModelRegistry.Error.decoderErrorModelNotMapped + throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: typeCodingKey.stringValue, codingKey: typeCodingKey, codingPath: arraycontainerCopy.codingPath) } modelLists.append(models) }