Alex
Alex

Reputation: 3981

PromiseKit catch multiple arguments

I'm integrating PromiseKit into our current system and I need the catch part of the chain to use 2 arguments. I'd like my catch to use error, fetcher instead of just error. What is the best way to do this?

infoPromise.then { objects in
    print(objects)
}.catch { error in /* error, fetcher in */
    print(error)
}

I've thought of including the fetcher as part of the userInfo dictionary on NSError, but I'd prefer it as a separate argument if possible.

Edit - More Info:

Here is the adapter I'm using. The fetcher is returned in the onError from my existing system.

- (AnyPromise *)fetchAll:(NSString *)entityName {
    return [AnyPromise promiseWithAdapterBlock:^(PMKAdapter  _Nonnull adapter) {
        [self fetchAll:entityName onComplete:^(NSArray *results) {
            adapter(results,nil);
        } onError:^(ICSHTTPFetcher *fetcher, NSError *error) {
            adapter(nil,error);
        }];
    }];
}

Upvotes: 0

Views: 806

Answers (1)

totiDev
totiDev

Reputation: 5275

I have not used promises much in Objective C. But if you are able to write the fetchAll function in swift then you could wrap the error and fetcher in a Swift error. Below is a rough example of this - it won't compile, it's just to give you an idea. It would probably be better to make a few more specific PromiseErrors indicating the actual error, than just the one as I have done and then not pass the NSError through.

enum PromiseErrors: Error {
    case fetchAll(NSError, ICSHTTPFetcher)
}

struct Test {
    func fetchAll() -> Promise<Any> {
        return fetchAll(entityName, onComplete: { results in
            return Promise(value: results)
        }, error: { fetcher, error in
            return Promise(error: PromiseErrors.fetchAll(error, fetcher))
        })
    }

    func test() {
        infoPromise.then { objects in
            print(objects)
        }.catch { error in
            if let case PromiseErrors.fetchAll(error, fetcher) = error {

            }
        }
    }
}

Upvotes: 1

Related Questions