Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid rethrows method compilation error #274

Open
fummicc1 opened this issue Nov 10, 2024 · 1 comment
Open

Avoid rethrows method compilation error #274

fummicc1 opened this issue Nov 10, 2024 · 1 comment

Comments

@fummicc1
Copy link
Collaborator

As of mockolo 2.1.1, rethrows method is not supported. The following mock produces compile error:

"error: call can throw, but the error is not handled; a function declared 'rethrows' may only throw if its parameter does"

/// @mockable
protocol ThrowableProtocol {
    func foo(action: () throws -> Int) rethrows
}

// ----- mock -----
class ThrowableProtocolMock: ThrowableProtocol {
    init() { }


    private(set) var fooCallCount = 0
    var fooHandler: ((() throws -> Int) throws -> ())?
    func foo(action: () throws -> Int) rethrows {
        fooCallCount += 1
        if let fooHandler = fooHandler {
            try fooHandler(action)
        }
        
    }
}

This is because rethrows only become throwable when parameter of the method has try keyword at the use site.

@sidepelican
Copy link
Collaborator

protocol P {
    func f(arg: () throws -> Void) rethrows
}

struct S: P {
    var fHandler: (() throws(any Error) -> Void) throws(any Error) -> Void = { _ in }
    func f<E: Error>(arg: () throws(E) -> Void) throws(E) {
        do {
            try fHandler(arg)
        } catch {
            throw error as! E
        }
    }
}

typed throws can solve this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants