From 41ce041a0dbbafae273b9874d27f9e7a1177f041 Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Thu, 14 Sep 2023 17:28:56 -0400 Subject: [PATCH] throw module decoding errors to link back to the source of the error --- .../OtherContainers/ModuleMolecule.swift | 8 +++---- .../Protocols/MoleculeDelegateProtocol.swift | 21 ++++++++----------- .../ReplacementMoleculeBehavior.swift | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/OtherContainers/ModuleMolecule.swift b/MVMCoreUI/Atomic/Molecules/OtherContainers/ModuleMolecule.swift index 6083ac5e..82525b01 100644 --- a/MVMCoreUI/Atomic/Molecules/OtherContainers/ModuleMolecule.swift +++ b/MVMCoreUI/Atomic/Molecules/OtherContainers/ModuleMolecule.swift @@ -23,7 +23,7 @@ open class ModuleMolecule: Container { super.set(with: model, delegateObject, additionalData) guard let moduleMoleculeModel = model as? ModuleMoleculeModel, - let moduleModel = delegateObject?.moleculeDelegate?.getModuleWithName(moduleMoleculeModel.moduleName) else { + let moduleModel = try? delegateObject?.moleculeDelegate?.getModuleWithName(moduleMoleculeModel.moduleName) else { // Critical error return } @@ -49,7 +49,7 @@ open class ModuleMolecule: Container { public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { guard let moduleMolecule = model as? ModuleMoleculeModel, - let moduleModel = delegateObject?.moleculeDelegate?.getModuleWithName(moduleMolecule.moduleName), + let moduleModel = try? delegateObject?.moleculeDelegate?.getModuleWithName(moduleMolecule.moduleName), let classType = ModelRegistry.getMoleculeClass(moduleModel), let height = classType.estimatedHeight(with: moduleModel, delegateObject) else { // Critical error @@ -60,7 +60,7 @@ open class ModuleMolecule: Container { public override class func nameForReuse(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> String? { guard let moduleMolecule = model as? ModuleMoleculeModel, - let moduleModel = delegateObject?.moleculeDelegate?.getModuleWithName(moduleMolecule.moduleName), + let moduleModel = try? delegateObject?.moleculeDelegate?.getModuleWithName(moduleMolecule.moduleName), let classType = ModelRegistry.getMoleculeClass(moduleModel), let name = classType.nameForReuse(with: moduleModel, delegateObject) else { // Critical error @@ -72,7 +72,7 @@ open class ModuleMolecule: Container { public override class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> [String]? { guard let moduleName = (model as? ModuleMoleculeModel)?.moduleName, - let _ = delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else { + let _ = try? delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else { if let errorObject = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: CoreUIErrorCode.ErrorCodeModuleMolecule.rawValue, domain: ErrorDomainNative, location: String(describing: self)) { error?.pointee = errorObject MVMCoreUILoggingHandler.addError(toLog: errorObject) diff --git a/MVMCoreUI/Atomic/Protocols/MoleculeDelegateProtocol.swift b/MVMCoreUI/Atomic/Protocols/MoleculeDelegateProtocol.swift index 6086849c..e78c0b1b 100644 --- a/MVMCoreUI/Atomic/Protocols/MoleculeDelegateProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/MoleculeDelegateProtocol.swift @@ -13,10 +13,11 @@ public protocol MoleculeDelegateProtocol: AnyObject { func getRootMolecules() -> [MoleculeModelProtocol] - /// returns a module for the corresponding module name. + /// Returns a raw module map for the corresponding module name if the key name exists. func getModuleWithName(_ name: String?) -> [AnyHashable: Any]? - func getModuleWithName(_ moleculeName: String) -> MoleculeModelProtocol? + /// Returns the decoded module for the corresponding module name if the key name exists. Throws if there is a decoding error. + func getModuleWithName(_ moleculeName: String) throws -> MoleculeModelProtocol? /// Notifies the delegate that the molecule layout update. Should be called when the layout may change due to an async method. Mainly used for list or collections. func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) //optional @@ -30,20 +31,16 @@ extension MoleculeDelegateProtocol { public func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) { } - public func getModuleWithName(_ moleculeName: String) -> MoleculeModelProtocol? { + public func getModuleWithName(_ moleculeName: String) throws -> MoleculeModelProtocol? { let moduleJSON: [AnyHashable: Any]? = getModuleWithName(moleculeName) - guard let moduleJSON = moduleJSON as? [String: Any], - let moleculeName = moduleJSON.optionalStringForKey("moleculeName"), - let modelType = ModelRegistry.getType(for: moleculeName, with: MoleculeModelProtocol.self) - else { return nil } + guard let moduleJSON = moduleJSON as? [String: Any] else { return nil } - do { - return try modelType.decode(jsonDict: moduleJSON as [String : Any]) as? MoleculeModelProtocol - } catch { - MVMCoreUILoggingHandler.logDebugMessage(withDelegate: "error: \(error)") + guard let moleculeName = moduleJSON.optionalStringForKey(KeyMoleculeName), + let modelType = ModelRegistry.getType(for: moleculeName, with: MoleculeModelProtocol.self) else { + throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: moduleJSON.optionalStringForKey(KeyMoleculeName)) } - return nil + return try modelType.decode(jsonDict: moduleJSON as [String : Any]) as? MoleculeModelProtocol } } diff --git a/MVMCoreUI/Behaviors/ReplacementMoleculeBehavior.swift b/MVMCoreUI/Behaviors/ReplacementMoleculeBehavior.swift index d5e6d86c..8daf3c8f 100644 --- a/MVMCoreUI/Behaviors/ReplacementMoleculeBehavior.swift +++ b/MVMCoreUI/Behaviors/ReplacementMoleculeBehavior.swift @@ -29,8 +29,8 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior { templateModel.printMolecules() for moleculeId in moleculeIds { - guard let replacementModel = delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId) else { continue } do { + guard let replacementModel = try delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId) else { continue } let didReplace = try templateModel.replaceMolecule(with: replacementModel) if !didReplace { MVMCoreLoggingHandler.addError(toLog: MVMCoreErrorObject(title: nil, messageToLog: "Failed to find '\(moleculeId)' in the current screen.", code: ErrorCode.viewControllerProcessingJSON.rawValue, domain: ErrorDomainSystem, location: String(describing: type(of: self)))!)