Swift throw from closure nested in a function

19,005

Solution 1

When you define closure that throws:

enum MyError: ErrorType {
    case Failed
}

let closure = {
    throw MyError.Failed
}

then type of this closure is () throws -> () and function that takes this closure as parameter must have the same parameter type:

func myFunction(completion: () throws -> ()) {
}

It this function you can call completion closure synchronous:

func myFunction(completion: () throws -> ()) throws {
    completion() 
}

and you have to add throws keyword to function signature or call completion with try!:

func myFunction(completion: () throws -> ()) {
    try! completion() 
}

or asynchronous:

func myFunction(completion: () throws -> ()) {
    dispatch_async(dispatch_get_main_queue(), { try! completion() })
}

In last case you will not be able to catch error.

So if completion closure in eventStore.requestAccessToEntityType method and the method itself does not have throws in its signature or if completion is called asynchronously then you can not throw from this closure.

I suggest you the following implementation of your function that passes error to callback instead of throwing it:

func insertEventToDefaultCalendar(event: EKEvent, completion: CalendarEventError? -> ()) {
    let eventStore = EKEventStore()
    switch EKEventStore.authorizationStatusForEntityType(.Event) {
    case .Authorized:
        do {
            try insertEvent(eventStore, event: event)
        } catch {
            completion(CalendarEventError.Failed)
        }

    case .Denied:
        completion(CalendarEventError.AccessDenied)

    case .NotDetermined:
        eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
            if granted {
                //insertEvent(eventStore)
            } else {
                completion(CalendarEventError.AccessDenied)
            }
        })
    default:
    }
}

Solution 2

Because throwing is synchronous, an async function that wants to throw must have an inner closure that throws, such as this:

func insertEventToDefaultCalendar(event :EKEvent, completion: (() throws -> Void) -> Void) {
    let eventStore = EKEventStore()
    switch EKEventStore.authorizationStatusForEntityType(.Event) {
    case .Authorized:
        do {
            try insertEvent(eventStore, event: event)
            completion { /*Success*/ }
        } catch {
            completion { throw CalendarEventError.Failed }
        }

        case .Denied:
            completion { throw CalendarEventError.AccessDenied }

        case .NotDetermined:
            eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
                if granted {
                    let _ = try? self.insertEvent(eventStore, event: event)
                    completion { /*Success*/ }
                } else {
                    completion { throw CalendarEventError.AccessDenied }
                }
        })
        default:
            break
    }
}

Then, at the call site, you use it like this:

   insertEventToDefaultCalendar(EKEvent()) { response in
        do {
            try response()
            // Success
        }
        catch {
            // Error
            print(error)
        }
    }

Solution 3

That's not possible in this case - that completion handler would have to be declared with throws (and the method with rethrows) and this one is not.

Note that all that throwing is just a different notations for NSError ** in Objective-C (inout error parameter). The Objective-C callback doesn't have an inout parameter so there is no way to pass the error up.

You will have to use a different method to handle errors.

In general, NSError ** in Obj-C or throws in Swift don't play well with asynchronous methods because the error handling works synchronously.

Share:
19,005
shannoga
Author by

shannoga

Lokking for a new challenge Lots of ideas. No time for all of them.

Updated on June 11, 2022

Comments

  • shannoga
    shannoga almost 2 years

    I have a function that throws an error, in this function I have a inside a closure that I need to throw the error from it's completion handler. Is that possible ?

    Here is my code so far.

    enum CalendarEventError: ErrorType {
        case UnAuthorized
        case AccessDenied
        case Failed
    }
    
    func insertEventToDefaultCalendar(event :EKEvent) throws {
        let eventStore = EKEventStore()
        switch EKEventStore.authorizationStatusForEntityType(.Event) {
        case .Authorized:
            do {
                try insertEvent(eventStore, event: event)
            } catch {
                throw CalendarEventError.Failed
            }
    
        case .Denied:
            throw CalendarEventError.AccessDenied
    
        case .NotDetermined:
            eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
                if granted {
                    //insertEvent(eventStore)
                } else {
                    //throw CalendarEventError.AccessDenied
                }
            })
        default:
        }
    }