Search code examples
siesta-swift

Siesta Swift: RequestError.Cause.RequestCancelled not conform to _ErrorCodeProtocol


I'm trying to check what exact error a request might throw by doing some custom checks and throw my own custom errors.

if let cause = resource.latestError?.cause {
    if case RequestError.Cause.RequestCancelled = cause {

    }
}

I get this error:

Argument type 'RequestError.Cause.RequestCancelled.Type' does not conform to expected type '_ErrorCodeProtocol'

Any ideas how I could check what the exact cause of the error is and then return my own custom errors?


Solution

  • Siesta’s error causes are open to extension and thus not an enum, so the if case syntax doesn’t work with them. (The compiler error you’re getting is because Swift thinks you’re trying to use case to extract an error code from an error that doesn’t have one.)

    Siesta’s error causes are instead a tree of distinct types. Instead of using if case, match error causes using is:

    if let cause = resource.latestError?.cause {
      if cause is RequestError.Cause.RequestCancelled {
    
      }
    }
    

    …or simply:

    if resource.latestError?.cause is RequestError.Cause.RequestCancelled {
    
    }
    

    …or if you need to assign the type-narrowed error to a variable so you can do something further with it:

    if let cause = resource.latestError?.cause as? RequestError.Cause.RequestCancelled {
    
    }