throw module decoding errors to link back to the source of the error

This commit is contained in:
Hedden, Kyle Matthew 2023-09-14 17:28:56 -04:00
parent 6758aed034
commit 41ce041a0d
3 changed files with 14 additions and 17 deletions

View File

@ -23,7 +23,7 @@ open class ModuleMolecule: Container {
super.set(with: model, delegateObject, additionalData) super.set(with: model, delegateObject, additionalData)
guard let moduleMoleculeModel = model as? ModuleMoleculeModel, 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 // Critical error
return return
} }
@ -49,7 +49,7 @@ open class ModuleMolecule: Container {
public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
guard let moduleMolecule = model as? ModuleMoleculeModel, 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 classType = ModelRegistry.getMoleculeClass(moduleModel),
let height = classType.estimatedHeight(with: moduleModel, delegateObject) else { let height = classType.estimatedHeight(with: moduleModel, delegateObject) else {
// Critical error // Critical error
@ -60,7 +60,7 @@ open class ModuleMolecule: Container {
public override class func nameForReuse(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> String? { public override class func nameForReuse(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> String? {
guard let moduleMolecule = model as? ModuleMoleculeModel, 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 classType = ModelRegistry.getMoleculeClass(moduleModel),
let name = classType.nameForReuse(with: moduleModel, delegateObject) else { let name = classType.nameForReuse(with: moduleModel, delegateObject) else {
// Critical error // Critical error
@ -72,7 +72,7 @@ open class ModuleMolecule: Container {
public override class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? { public override class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
guard let moduleName = (model as? ModuleMoleculeModel)?.moduleName, 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)) { if let errorObject = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: CoreUIErrorCode.ErrorCodeModuleMolecule.rawValue, domain: ErrorDomainNative, location: String(describing: self)) {
error?.pointee = errorObject error?.pointee = errorObject
MVMCoreUILoggingHandler.addError(toLog: errorObject) MVMCoreUILoggingHandler.addError(toLog: errorObject)

View File

@ -13,10 +13,11 @@ public protocol MoleculeDelegateProtocol: AnyObject {
func getRootMolecules() -> [MoleculeModelProtocol] 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(_ 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. /// 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 func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) //optional
@ -30,20 +31,16 @@ extension MoleculeDelegateProtocol {
public func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) { } public func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) { }
public func getModuleWithName(_ moleculeName: String) -> MoleculeModelProtocol? { public func getModuleWithName(_ moleculeName: String) throws -> MoleculeModelProtocol? {
let moduleJSON: [AnyHashable: Any]? = getModuleWithName(moleculeName) let moduleJSON: [AnyHashable: Any]? = getModuleWithName(moleculeName)
guard let moduleJSON = moduleJSON as? [String: Any], guard let moduleJSON = moduleJSON as? [String: Any] else { return nil }
let moleculeName = moduleJSON.optionalStringForKey("moleculeName"),
let modelType = ModelRegistry.getType(for: moleculeName, with: MoleculeModelProtocol.self)
else { return nil }
do { guard let moleculeName = moduleJSON.optionalStringForKey(KeyMoleculeName),
return try modelType.decode(jsonDict: moduleJSON as [String : Any]) as? MoleculeModelProtocol let modelType = ModelRegistry.getType(for: moleculeName, with: MoleculeModelProtocol.self) else {
} catch { throw ModelRegistry.Error.decoderErrorModelNotMapped(identifer: moduleJSON.optionalStringForKey(KeyMoleculeName))
MVMCoreUILoggingHandler.logDebugMessage(withDelegate: "error: \(error)")
} }
return nil return try modelType.decode(jsonDict: moduleJSON as [String : Any]) as? MoleculeModelProtocol
} }
} }

View File

@ -29,8 +29,8 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
templateModel.printMolecules() templateModel.printMolecules()
for moleculeId in moleculeIds { for moleculeId in moleculeIds {
guard let replacementModel = delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId) else { continue }
do { do {
guard let replacementModel = try delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId) else { continue }
let didReplace = try templateModel.replaceMolecule(with: replacementModel) let didReplace = try templateModel.replaceMolecule(with: replacementModel)
if !didReplace { 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)))!) 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)))!)