If you want to catch all AppErrors , you can use this pattern:
catch is AppErrors
If you are looking for a more exact match, it seems like it is quickly becoming ugly.
This will allow us to catch specific cases of AppErrors :
catch let error as AppErrors where error == .NotFound || error == .AlreadyUsed
There is also this syntax that seems to work:
catch let error as AppErrors where [.NotFound, .AlreadyUsed].contains(error)
For completeness, I will also add this parameter, which allows us to catch errors of two different types, but this does not allow us to indicate which case is in these types:
catch let error where error is AppErrors || error is NSError
Finally, based on the fact that everything that we catch will comply with the ErrorType protocol, we can clear the second and third examples that I provided with the ErrorType extension and use this together, where is the where clause in our catch :
extension ErrorType { var isFooError: Bool { guard let err = self as? AppErrors else { return false } return err == .NotFound || err == .AlreadyUsed } }
And just catch it like this:
catch let error where error.isFooError
source share