Error Handling

The SDK is structured into multiple modules and each module will raise its own high level exception type which only has one variant ExceptionDetails(source:SdkException).

This is required due to the way how the language bindings of Rust are being generated and how Exceptions are being handled differently across multiple platforms.

Exception Categories

Exception Type

Description

SdkManager

SdkManager related Exception

SdkConfigurationException

SDK Configuration related Exception

DipException

There was a DIP related exception

IdentityException

Identity related exception

IdpException

Identity Protection related exception

InstanceDiscoveryException

Instance Discovery related exception

SubscriptionsException

Subscriptions related exceptions

TokenException

Token Management related exceptions

CallbackException

Exception thrown by the client’s callback implementation for HTTPCallbackProtocol and StorageCallbackProtocol

The SdkException type contains details about the concrete source of the exception,

Handling Exceptions properly

In the first step, you catch the top-level type,, then you evaluate the “source” field of that top-level type further.

//Extract the top level category
public func execute_and_handle_api_request<T>(operation: () throws -> T?) -> T? {
        do {
            return try operation()
        } catch let SdkManagerException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let SdkConfigurationException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let DipException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let IdentityException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let IdpException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let InstanceDiscoveryException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let SubscriptionsException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let TokenException.ExceptionDetails(source) {
            print(self.handle_error(error: source))
        } catch let error as SdkException {
            print(self.handle_error(error: error))
        } catch {
            print("\(error)")        }
        return nil
}

//Handle the specific SdkException
func handle_error(error: SdkException) -> String {
        switch error {
        case let .Network(reason):
            return "Network Error: \(reason)"
        case .UserAlreadyExists:
            return "Account creation failed: User already exists."
        case .UserNotFound:
            return "Account not found."
        ...
}

Last updated