Skip to main content
Proper error handling is crucial for building reliable applications with S2. The SDK provides structured error types, automatic retry mechanisms, and clear patterns for handling different failure scenarios.

Error Types

S2Error

The top-level error type for SDK operations:

Error Categories

1

API Errors

Server-side errors with specific error codes:
2

Client Errors

SDK-side errors from session management:
3

Network Errors

Transport and connectivity issues:
4

Validation Errors

Input validation failures:

Retryable vs Terminal Errors

Retryable Errors

These errors are automatically retried by sessions:
Retryable conditions:
  • Acknowledgement timeout
  • Server disconnection
  • Heartbeat timeout (read sessions)
  • Transient network errors
  • Server unavailability (503)
  • Gateway timeout (504)

Terminal Errors

These errors end the operation immediately:
  • Invalid input (400)
  • Permission denied (403)
  • Resource not found (404)
  • Conflict (409)
  • Quota exhausted (403)
  • Session closed
  • Session dropped
Terminal errors indicate a problem that cannot be resolved by retrying. Handle them explicitly in your application logic.

Automatic Retry with Backoff

Sessions implement exponential backoff with jitter:

Backoff Behavior

From retry.rs:79-97:
Jitter is randomized (0-100% of base delay) to prevent thundering herd issues when multiple clients retry simultaneously.

Retry Reset

Backoff resets on successful operations:

Error Handling Patterns

Pattern 1: Basic Error Handling

Pattern 2: Graceful Degradation

Pattern 3: Circuit Breaker

Pattern 4: Error Aggregation

Append-Specific Errors

Append Condition Failed

When using fencing tokens or sequence number matching:

Read-Specific Errors

Heartbeat Timeout

Validation Errors

Catch validation errors early:

Best Practices

Let sessions retry: Don’t implement your own retry logic for transient errors. Sessions handle this automatically.
Handle terminal errors explicitly: Check for specific error codes like permission_denied or quota_exhausted and handle them appropriately.
Log context with errors: Include relevant context (basin, stream, operation) when logging errors for easier debugging.
Use structured error handling: Match on error types and codes rather than string parsing.
Always call close() on sessions, even after errors, to ensure proper cleanup and resource release.
Monitor error rates: Track error types and frequencies to detect systemic issues early.