Skip to main content

Handling Errors and Retries

Handling Errors and Retries

When integrating with multiple APIs and services, handling errors gracefully is crucial for building resilient operations. Fiscus SDK provides a comprehensive system for error handling and custom retry logic, allowing you to manage failures, automate retries, and build fault-tolerant workflows.

Error Handling in Task Execution

Errors can occur during task execution for various reasons, such as network issues, API rate limits, authentication failures, or incorrect parameters. Fiscus SDK offers built-in mechanisms to manage these errors effectively.

Error Callbacks

Error callbacks help you manage failures by providing a detailed error message and context when an API operation fails. By using error callbacks, you can log the failure, notify users, or trigger retries.

Example: Using an Error Callback

  1. Define an Error Callback

    Define a callback function to capture and handle error information.

def fiscus_on_error(info):
print(f"Error occurred: {info['message']}")
  1. Attach the Callback

    Attach the error callback when executing an API operation.

callbacks = {'fiscus_on_error': fiscus_on_error}

response = client.execute('QuickBooks', 'create_invoice', {
'customer_id': '12345',
'amount': 150.00,
'due_date': '2024-10-31'
}, callbacks=callbacks, user=user)
  1. Handle Error

    If the operation fails, the error message is captured and printed through the callback.

if not response.success:
print(f"Error: {response.error_message}")

Exception Handling

While callbacks are ideal for handling specific task failures, you can also catch errors raised by the SDK during execution, allowing you to respond more programmatically.

Common Exceptions

  • FiscusAuthenticationError: Raised when the API operation requires authentication, and the user is not authenticated.
  • FiscusAuthorizationError: Raised when the user does not have permission to perform the operation.
  • FiscusValidationError: Raised when the provided parameters do not meet the required format or constraints.

Example: Handling Authentication Errors

from fiscus import FiscusAuthenticationError

try:
response = client.execute('QuickBooks', 'create_invoice', {
'customer_id': '12345',
'amount': 150.00,
'due_date': '2024-10-31'
}, user=user)

if response.success:
print('Invoice created successfully!')
else:
print(f"Error: {response.error_message}")

except FiscusAuthenticationError as e:
print(f"Authentication error: {e}")
# Trigger authentication and retry
user.authenticate_connector('QuickBooks')
retry_response = client.execute('QuickBooks', 'create_invoice', {...}, user=user)
if retry_response.success:
print('Invoice created successfully after authentication!')
else:
print(f"Error: {retry_response.error_message}")

In this example, we catch authentication errors, handle them by re-authenticating the user, and retry the operation.

 

Custom Retry Logic for Resilient Operations

When dealing with unstable services or APIs, Fiscus SDK allows you to customize retry behavior to improve the reliability of your operations. By specifying custom retry logic, you can ensure that operations automatically retry after failure, with controlled intervals between retries.

Setting Custom Retry Logic

You can configure retry logic when initializing the FiscusClient by setting the retries and backoff_factor parameters. Retries define how many times the SDK should retry a failed operation, while the backoff_factor controls the delay between retries. The delay increases exponentially with each retry, helping to prevent overwhelming the API with requests.

Example: Configuring Retries

  1. Initialize Client with Retry Logic

    Initialize the FiscusClient with custom retry logic parameters.

client = FiscusClient(
api_key='YOUR_FISCUS_API_KEY',
retries=5,
backoff_factor=1.0
)
  1. Execute Operation with Retry Logic

    Perform an API operation, allowing the SDK to automatically retry if the operation fails.

response = client.execute('ExternalAPI', 'fetch_data', {...}, user=user)

if response.success:
print('Data fetched successfully!')
else:
print(f"Error: {response.error_message}")

In this example, the SDK will retry the operation up to five times if it fails, with an increasing delay between each retry.

Advanced Retry Customization

In addition to setting retries and backoff factors globally, you can override the retry behavior for specific operations. This gives you granular control over how retries are handled based on the operation type or the API being called.

Example: Overriding Retry Logic for a Specific Operation

  1. Define Custom Retry Logic

    You can override the retry logic for a particular API operation by specifying custom retry parameters during execution.

retry_response = client.execute('UnreliableAPI', 'critical_action', {
'param1': 'value1',
'param2': 'value2'
}, retries=10, backoff_factor=2.0, user=user)
  1. Handle the Response

    After the SDK retries the operation, handle the success or failure accordingly.

if retry_response.success:
print('Operation completed successfully after retries!')
else:
print(f"Error after retries: {retry_response.error_message}")

This example demonstrates how to apply custom retry logic to a single operation, retrying it 10 times with a backoff factor of 2.0.

Pro Tip: Use Error Callbacks and Retries Together

Combining Error Callbacks with Retry Logic:

  • Combining error callbacks with retry logic provides a powerful mechanism for handling failures.
  • Use an error callback to log failures or notify the user while the SDK continues retrying the operation behind the scenes.
  • This makes your application more resilient and user-friendly, even in the face of transient API issues.

By utilizing Fiscus SDK’s error handling and retry capabilities, you can build robust workflows that automatically recover from transient issues and maintain operational continuity.