Skip to main content

Overview

Grain SDK handles most errors automatically with retries and fallbacks. However, understanding error scenarios helps you build robust applications.

Automatic Error Handling

The SDK automatically handles common failures: Network Errors: Retries with exponential backoff Rate Limits: Backs off and retries later Server Errors: Retries failed requests Invalid Data: Logs warning, continues You don’t need to handle these explicitly.

Manual Error Handling

For critical operations, catch errors explicitly:

Track Method Errors

The track method is fire-and-forget by default:
With flush: true, it returns a Promise you can catch:

Configuration Errors

Config methods degrade gracefully:
Key point: Your app never breaks due to config failures. Cached or default values always available.

React Hook Error Handling

Hooks provide error states:

Authentication Errors

JWT token errors are thrown, so catch them:

Network Offline

When offline, events queue locally:
No special handling needed. SDK manages the queue.

Debug Mode

Enable debug mode to see detailed error information:

Error Types

Common errors you might encounter: Network Errors: Failed to fetch, Network request failed
  • Usually temporary - SDK retries automatically
Auth Errors: Unauthorized, Invalid token
  • Check your auth configuration
Validation Errors: Invalid event, Missing required field
  • Review your event structure
Rate Limit: Too many requests
  • SDK backs off automatically

Handling Critical Failures

For must-not-fail scenarios:

Fallback Strategies

Provide fallbacks for failed operations:

Best Practices

1. Don’t Block UI: Never wait indefinitely for analytics
2. Provide Defaults: Always have fallback content 3. Log Don’t Crash: Log errors, don’t throw them to users 4. Retry Critical Events: Use flush: true and manual retries for important events 5. Monitor Errors: Check dashboard for error patterns

Next Steps

Configuration

Configure retry behavior

Security

Secure error handling