concept Error in category swift

appears as: Error
Swift in Depth

This is an excerpt from Manning's book Swift in Depth.

  • Even though errors are usually enums, any type can implement the Error protocol.
  • This is a good start, but there’s a problem; the processPayment method can return multiple Error types, such as a PaymentError or an AccountError, and inside the method, there’s a call to moneyAPI.transfer(amount:, from:, to:), which can also throw errors. Mixing errors means that you can’t return a Result containing a specific PaymentError. You may want to loosen up your code by mixing and matching multiple error types in combination with the Result type. We can introduce a new error type that can store other errors, but there’s a quicker way. What we can do is pin the Error generic to itself in the definition of Result, such as Result<(), Error>; this will allow us to use errors at runtime. Let’s see how this works by completing the processPayment method.

    Listing 11.14. Returning multiple error types
    func processPayment(fromAccount: Account, toAccount: Account,
         amountInCents:Int) -> Result<(), Error> {               #1
    
        guard amountInCents > 0 else {
            return Result.failure(PaymentError.amountTooLow)     #2
        }
    
        guard fromAccount != toAccount else {
            return Result.failure(AccountError.invalidAccount)   #2
        }
    
        // Process payment
        do {
            try moneyAPI.transfer(amount: amountInCents, from: fromAccount, to:
         toAccount)
            return Result.success(())
        } catch {
            return Result.failure(error)                         #3
        }
    }

    Once we set the Error type to Error itself, we can put all kinds of error types inside Result. The downside is that we don’t know the error type until runtime, similar to throwing methods.

    Using errors dynamically will speed up your development when working with Result. Then, once you’re ready, you can slowly replace each Error inside Result to a specific error, which you can do with mapError(). Working with specific errors will make your code more rigid, but, in return, you gain robustness by having error-type knowledge at compile-time.

    When we set the Error constraint to Error itself—as opposed to a concrete type such as PaymentError—we state that Error conforms to itself. Having a protocol conform to itself normally isn’t possible for other protocols, but Error is an exception. One reason that this is possible is because the Error protocol definition is empty—it is a marker for error handling, if you will. The Swift 5 compiler can use that knowledge to apply some optimizations that allow for this rare exception.

    Here’s a controversial thought: you can use the Result type for all the error handling in your project. You get more compile-time benefits, but at the price of more difficult programming. Error handling is more rigid with Result, but your code will be safer and stricter as a reward. And if you want to speed up your work a little, you can always create a Result type with the failure case constrained to the Error protocol.