0

I have a maybe silly question. I coming from programming languages with exception handling, e.g. C++ and C#. At the time of Swift 1 I was confronted with the Cocoa Style with error objects (which somewhat reminded me at the ErrorObject in Microsoft COM).

After a time I began to use for my side project app an error handling pattern, in which I used a function parameter (closure) for error handling:

public typealias FailureBlock = (error:NSError!)->Void
JournalEntryStorage.instance.createJournalEntry(journalInfo, failure:
{
    error in
    NSLog("Watch App saveNewEntryInJournal: \(error.description)")           
})

This solution seems nice at the time, because the function parameter forces me to code the error handling logic or I could simply make a call to a passed error handler function.

Now we have a solid error handling in place with Swift 2. Should I change my code completely to the new pattern? There is a lot of code.

How do you deal with your (legacy) code and the new error pattern?

By the way: I found the original Cocoa way with passing an error pointer and dealing with it so ugly in Swift 1. Maybe my method is more ugly but for me it seemed working.

ChaosSpeeder
  • 3,468
  • 5
  • 30
  • 38
  • 1
    Depends. If your `createJournalEntry` is async, you should leave it as it is. If not, I would switch to the new error handling. If it's legacy code, it doesn't make sense to rewrite it just because of new error handling. Does it work? Leave it. Are you going to touch it because of another reason? You can change it, piece by piece. Call it transition period. BTW in your `FailureBlock`, change `(error: NSError!)` to `(error: ErrorType)`, so, you can pass `NSError` or new errors as well. – zrzka Jul 30 '15 at 17:24
  • Thank you for your advise. I have decided for following solution. a) I leave the pattern for all async function calls. b) I convert all synchronous functions to the new error handling scheme. First impression. The code is much more readable. thank you – ChaosSpeeder Aug 01 '15 at 11:39

1 Answers1

0

I have decided for following solution and close the question:

a) I leave my old error handling pattern for all asynchronous function calls. b) I am converting all synchronous functions to the new error handling scheme.

First impression. The code is much more readable.

ChaosSpeeder
  • 3,468
  • 5
  • 30
  • 38