From 586a4696e42d00a2d27054780bc692b7e3824184 Mon Sep 17 00:00:00 2001 From: Dinesh Harjani Date: Fri, 11 Nov 2022 13:42:56 +0000 Subject: [PATCH] Improved Error Reporting when Selecting DFU File I noticed we had more information in the code than towards the user, which is not helpful when debugging issues. So now we've added some descriptions for common errors, and they're also exposed to the user. --- Example/Example/Util/McuMgrPackage.swift | 20 ++++++++++++++-- .../FirmwareUpgradeViewController.swift | 7 +++++- Source/McuMgrManifest.swift | 23 +++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Example/Example/Util/McuMgrPackage.swift b/Example/Example/Util/McuMgrPackage.swift index 135577d..62e7cd3 100644 --- a/Example/Example/Util/McuMgrPackage.swift +++ b/Example/Example/Util/McuMgrPackage.swift @@ -71,8 +71,24 @@ public struct McuMgrPackage { extension McuMgrPackage { - enum Error: Swift.Error { - case deniedAccessToScopedResource, notAValidDocument, unableToAccessCacheDirectory, manifestFileNotFound, manifestImageNotFound + enum Error: Swift.Error, LocalizedError { + case deniedAccessToScopedResource, notAValidDocument, unableToAccessCacheDirectory + case manifestFileNotFound, manifestImageNotFound + + var errorDescription: String? { + switch self { + case .deniedAccessToScopedResource: + return "Access to Scoped Resource (iCloud?) Denied." + case .notAValidDocument: + return "This is not a valid file for DFU." + case .unableToAccessCacheDirectory: + return "We were unable to access the Cache Directory." + case .manifestFileNotFound: + return "DFU Manifest File not found." + case .manifestImageNotFound: + return "DFU Image specified in Manifest not found." + } + } } } diff --git a/Example/Example/View Controllers/Manager/FirmwareUpgradeViewController.swift b/Example/Example/View Controllers/Manager/FirmwareUpgradeViewController.swift index be30486..cae5c2f 100644 --- a/Example/Example/View Controllers/Manager/FirmwareUpgradeViewController.swift +++ b/Example/Example/View Controllers/Manager/FirmwareUpgradeViewController.swift @@ -274,6 +274,7 @@ extension FirmwareUpgradeViewController: FirmwareUpgradeDelegate { eraseSwitch.isEnabled = true status.textColor = .systemRed status.text = "\(error.localizedDescription)" + status.numberOfLines = 0 dfuSpeed.isHidden = true } @@ -290,6 +291,7 @@ extension FirmwareUpgradeViewController: FirmwareUpgradeDelegate { eraseSwitch.isEnabled = true status.textColor = .primary status.text = "CANCELLED" + status.numberOfLines = 0 dfuSpeed.isHidden = true } @@ -341,6 +343,7 @@ extension FirmwareUpgradeViewController: UIDocumentMenuDelegate, UIDocumentPicke status.textColor = .primary status.text = "READY" + status.numberOfLines = 0 actionStart.isEnabled = true dfuSwapTime.text = "\(dfuManagerConfiguration.estimatedSwapTime)s" @@ -351,10 +354,12 @@ extension FirmwareUpgradeViewController: UIDocumentMenuDelegate, UIDocumentPicke dfuByteAlignment.numberOfLines = 0 } catch { print("Error reading hash: \(error)") + fileName.text = url.lastPathComponent fileSize.text = "" fileHash.text = "" status.textColor = .systemRed - status.text = "INVALID FILE" + status.text = "Error Loading File: \(error.localizedDescription)" + status.numberOfLines = 0 actionStart.isEnabled = false } } diff --git a/Source/McuMgrManifest.swift b/Source/McuMgrManifest.swift index 9db2c3b..7160cb4 100644 --- a/Source/McuMgrManifest.swift +++ b/Source/McuMgrManifest.swift @@ -27,7 +27,7 @@ public struct McuMgrManifest: Codable { public init(from url: URL) throws { guard let data = try? Data(contentsOf: url), let stringData = String(data: data, encoding: .utf8) else { - throw Error.unableToImport + throw Error.unableToRead } let stringWithoutSpaces = String(stringData.filter { !" \n\t\r".contains($0) }) @@ -35,7 +35,11 @@ public struct McuMgrManifest: Codable { guard let cleanData = modString.data(using: .utf8) else { throw Error.unableToParseJSON } - self = try JSONDecoder().decode(McuMgrManifest.self, from: cleanData) + do { + self = try JSONDecoder().decode(McuMgrManifest.self, from: cleanData) + } catch { + throw Error.unableToDecodeJSON + } } } @@ -106,7 +110,18 @@ extension McuMgrManifest { extension McuMgrManifest { - enum Error: Swift.Error { - case unableToImport, unableToParseJSON + enum Error: Swift.Error, LocalizedError { + case unableToRead, unableToParseJSON, unableToDecodeJSON + + var errorDescription: String? { + switch self { + case .unableToRead: + return "Unable to Read Manifest JSON File." + case .unableToParseJSON: + return "Unable to Parse Manifest JSON File." + case .unableToDecodeJSON: + return "Unable to Decode Manifest JSON File." + } + } } }