Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Error Reporting when Selecting DFU File #88

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Example/Example/Util/McuMgrPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ extension FirmwareUpgradeViewController: FirmwareUpgradeDelegate {
eraseSwitch.isEnabled = true
status.textColor = .systemRed
status.text = "\(error.localizedDescription)"
status.numberOfLines = 0
dfuSpeed.isHidden = true
}

Expand All @@ -290,6 +291,7 @@ extension FirmwareUpgradeViewController: FirmwareUpgradeDelegate {
eraseSwitch.isEnabled = true
status.textColor = .primary
status.text = "CANCELLED"
status.numberOfLines = 0
dfuSpeed.isHidden = true
}

Expand Down Expand Up @@ -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"
Expand All @@ -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
}
}
Expand Down
23 changes: 19 additions & 4 deletions Source/McuMgrManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ 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) })
let modString = Self.LoadAddressRegEx.stringByReplacingMatches(in: stringWithoutSpaces, options: [], range: NSRange(stringWithoutSpaces.startIndex..<stringWithoutSpaces.endIndex, in: stringWithoutSpaces), withTemplate: " ")
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
}
}
}

Expand Down Expand Up @@ -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."
}
}
}
}