[swift-users] Can't get Error pattern matching to work cross framework and command line tool

Joakim Hassila joj at mac.com
Mon May 30 23:44:52 CDT 2016

Ok, I am stumped and wanted to see if anyone had any ideas on how to troubleshoot this.

Short background:
First off, I do have throwing/do/catch working perfectly fine in Playgrounds as well as in standalone apps as well as in an app that links with one of my own test frameworks, so I would hope this is not a simple user error.

I’m testing this on OS X w. XCode 7.3.1 using Swift 2.2 all with Debug builds.

I have a command line tool project "X" that links with two of my own Swift frameworks, “Y" and “Z". Z also links with a pure C-language library, “W” using a module map.
All of this works fine, as X can use entities from both Y and Z perfectly fine, including indirect access of the C-library beneath.

So the issue is when I am looking at throwing errors from Z, as X can’t properly pattern match the errors.

Short snippet showing relevant code and output from a testrun:

From the command line tool X:
func localThrow () throws -> Void
    throw Transaction.Error.NotFound

    try localThrow() // try throwing from embedded local function
} catch Transaction.Error.NotFound { // this matches as expectd
    print("Transaction not found (local)")
} catch  {
    print("Unknown error (local)")

let transaction2 = Transaction() 

do {
    try transaction2.errorNow() // try throwing from framework
} catch Transaction.Error.NotFound { // this never matches!
    print("Transaction not found (framework)")
} catch {
    print("Unknown error (framework)")

In the framework Z, we have:
import W

public class Transaction {
// ...

public extension Transaction {
    public final func errorNow() throws -> Void
        throw Transaction.Error.NotFound

and the output when run is:
Transaction not found (local)
Unknown error (framework) 

Setting a breakpoint and inspecting ‘error’ shows the following when stepping over the call, and single stepping in the debugger shows that it throws properly, it is just the matching that seems to fail. The Xcode lldb inspector shows the error value as:

error = (Z.Transaction.Error) NotFound

When single-stepping the local call, ‘error’ does not seem to be populated properly but it matches as it should.

I tried minimizing it with a simple app (not command line though) and framework, but there it works as expected of course…

Anyone have any ideas what might be the problem?



More information about the swift-users mailing list