Skip to main content

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 TypeDescription
SdkManagerExceptionSdkManager related exception
SdkConfigurationExceptionSDK Configuration related exception
DipExceptionThere was a DIP related exception
IdentityExceptionIdentity related exception
IdpExceptionIdentity Protection related exception
InstanceDiscoveryExceptionInstance Discovery related exception
SubscriptionsExceptionSubscriptions related exceptions
TokenExceptionToken Management related exceptions
CallbackExceptionException thrown by the client’s callback implementation for HTTPCallbackProtocol and StorageCallbackProtocol
InAppPaymentExceptionInApp Payments related exception
LicenseExceptionExceptions related to license validity

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.

// Executes the given operation and handles known SDK exceptions.
// Returns the operation result or nil if an error occurred.
public func executeAndHandleAPIRequest<T>(operation: () throws -> T?) -> T? {
do {
return try operation()
} catch let SdkManagerException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let SdkConfigurationException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let DipException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let IdentityException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let IdpException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let InstanceDiscoveryException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let SubscriptionsException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let TokenException.ExceptionDetails(source) {
print(self.handleError(error: source))
} catch let error as SdkException {
print(self.handleError(error: error))
} catch {
print("\(error)")
}
return nil
}

// Handle the specific SdkException
func handleError(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."
...
}