NordicSemiconductor / IOS-DFU-Library

OTA DFU Library for Mac and iOS, compatible with nRF5x SoCs
http://www.nordicsemi.com
BSD 3-Clause "New" or "Revised" License
525 stars 214 forks source link

Adding optional error handler parameter to DFUFirmware initializers. #463

Closed appfactoryCo closed 2 years ago

appfactoryCo commented 3 years ago

Hello there,

Thank you for creating this useful library.

I was trying to create a firmware file by passing a url. I noticed that I didn't get enough information about the kind of error that happened when using the DFUFirmware initializer. For example, when I passed it a url to a zip file like this:

let firmware = DFUFirmware(urlToZipFile: firmwareUrl)

Then, I found this error in the logs:

Error while creating ZIP stream: The operation couldn’t be completed. (NordicDFU.DFUStreamZipError error 0.)

I had no idea what that meant, and there was no initializer that would give my app an error. So, I added errorHandler parameter to the DFUFirmware initializers to get some errors back to my app:

@objc convenience public init?(urlToZipFile: URL, errorHandler: ((String) -> ())?)

For example, now I can do something like this:

let firmware = DFUFirmware(urlToZipFile: firmwareUrl, errorHandler: { [weak self]
                    errorMsg in
                    DispatchQueue.main.async { // Handle creating firmware errors in main thread
                        self?.raiseAlert(title: "Sorry", message: errorMsg)
                    }
                })

or

let firmware = DFUFirmware(urlToZipFile: firmwareUrl, errorHandler: nil)

Doing so helped me figure out what the error was. It was "No manifest file found," and now I can let the app display this error back to the user.

I hope this little contribution is useful.

Thanks a lot.

CLAassistant commented 3 years ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Badr Bujbara seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

philips77 commented 2 years ago

I think we'll close this PR and try coming up with something similar to solve your problem. The init may throw an error instead of returning nil.

I'll just leave it open for now not to forget about it.

philips77 commented 2 years ago

Closed with #486.