Error handling

The majority of the Client APIs are request based calls returning completion handlers in response.

All request based APIs return, at a minimum, the following completion handlers:

    onSuccess
    onNoConnection
    onInvalidToken
    onClientError

Additional completion handlers are defined on a call by call basic. For example registerUser additionally returns the ValidationError & UserAlreadyRegisteredError response handlers.

Each of the error type completion handlers returns a related error object, details of which can be found here Errors section.

onSuccess

The success block takes two forms:

returning an empty block

    () -> Void

returning an object

    (_ user : [Transaction]) -> Void

onInvalidToken

If any call fails with an InvalidTokenException, check the error’s Action to determine what action should be taken next:

Swift3

    ...
    onInvalidToken: { (error) in
        if error.action == InvalidTokenError.Action.login {
            // get a new access token and authenticate again
        } else { // InvalidTokenError.Action.retry
           // retry failing call
        }
    }
    ...

ObjC

     ...
     onInvalidToken:^(InvalidTokenError * _Nonnull error) {
         switch (error.action) {
             case InvalidTokenErrorCodeLogin:
                 // Stuff
                 break;

             case InvalidTokenErrorCodeRetry:
                 // Stuff
                 break;

             default:
                 break;
         }
     }
     ...

Documentation

A small number of API calls throw errors and should be called with a try or do-catch block in Swift or with an NSError in Objective C e.g.

Swift3

    do {
        try SDK.getClient.initialize()
    }
    catch (let exception) {
        // Handle exception
    }

ObjC

    NSError *error;
    [SDK.getClient initialize:&error];
    if (error) {
        // Handle error
    }