Error Handling
Exception Categories
Handling Exceptions properly
//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