Is it possible to catch a particular error enum case and also preserve the entire error value? Example:
enum CookingError: Error {
case missingIngredient(Ingredient, quantity: Int)
case kitchenOnFire
}
do {
try makeFood()
}
catch CookingError.missingIngredient {
handleMissingIngredient(error) // This doesn't work, because 'error' isn't defined
}
I've tried the following, and they don't work:
catch let error as CookingError {
// Not specific enough; I only want to catch missingIngredient errors.
}
catch let error as CookingError.missingIngredient {
// nope, because an enum case isn't a type
}
catch let error where case CookingError.missingIngredient = error {
// nope, compiler doesn't like 'case' here
}
The only thing I can think of is to reconstruct the whole error value:
catch CookingError.missingIngredient(let ingredient, let quantity) {
let error = CookingError.missingIngredient(ingredient, quantity: quantity)
handleMissingIngredient(error)
}
Is handleMissingIngredient immediately destructuring the CookingError internally? If so, what does it do with error values that aren't missingIngredient? And why not just pass the destructured data in directly?
And if handleMissingIngredientdoesn't destructure the error internally, does it need to be passed at all?
Does handleMissingIngredient immediately destructuring the CookingError internally?
Yeah, it is destructuring it internally, but then it may also rethrow the error depending on other conditions, so it's nice to have the full error still around.