1

I want to build a fully Swift error handling and propagation system that I can use throughout an entire application. The implementation is fairly straightforward, if you have something like this:

enum AnnouncerError: Error {
  /// A network error occured
  case networkError
  /// The program was unable to unwrap data from nil to a non-nil value
  case unwrapError
  /// The parser was unable to validate the XML
  case validationError
  /// The parser was unable to parse the XML
  case parseError
}

I can get the type of the error in a delegate function like this using a simple switch statement:

func feedFinishedParsing(withFeedArray feedArray: [FeedItem]?, error: AnnouncerError?) {
  // unwrap the error somewhere here
  switch error {
  case .networkError:
    print("Network error occured")
  case .parseError:
    print("Parse error occured")
  case .unwrapError:
    print("Unwrap error occured")
  default:
    print("Unknown error occured")
  }
}

However, I have the additional data from a particular case in the error enum and that's when the problems arise:

enum AnnouncerError: Error {
  /// A network error occured
  case networkError(description: String) //note this line
  /// The program was unable to unwrap data from nil to a non-nil value
  case unwrapError
  /// The parser was unable to validate the XML
  case validationError
  /// The parser was unable to parse the XML
  case parseError
}

How should I get the description string from the Error type if I call the delegate method with a .networkError?

As an extension, if there is no straightforward way to directly get the description from a .networkError, should I continue using the current architecture where the delegate method has an optional (nullable) Error type where I can check for the type of the error, or should I use a completely different architecture such as a try-catch system and if so, how should I implement it?

Pan Ziyue
  • 2,287
  • 2
  • 16
  • 17
  • Possibly related: [Swift 3 errors with additional data](http://stackoverflow.com/questions/41202869/swift-3-errors-with-additional-data) and [How to provide a localized description with an Error type in Swift?](http://stackoverflow.com/questions/39176196/how-to-provide-a-localized-description-with-an-error-type-in-swift). – Martin R Dec 24 '16 at 13:36

1 Answers1

2

If you would like to access the description property of networkError case , you do not need to do too much.

func feedFinishedParsing(withFeedArray feedArray: [FeedItem]?, error: AnnouncerError?) {
    // unwrap the error somewhere here
    switch error {
    // Just declare description in the case, and use it if u need it.
    case .networkError(let description):
        print("Network error occured")
        print(description)
    case .parseError:
        print("Parse error occured")
    case .unwrapError:
        print("Unwrap error occured")
    default:
        print("Unknown error occured")
    }
}

Of course, you can build a robust machine for error handling, but if you just wanna access this property, the above solution will do the trick.

Regardless, i would recommend to read further into Swift pattern matching, what explains in detail the advanced usage of the switch language element is Swift.

dirtydanee
  • 6,081
  • 2
  • 27
  • 43