Overview
Robust error handling is critical for long-running workflows. The package provides automatic retry mechanisms, exception tracking, and failure handling strategies.Exception Types
Retryable Exceptions
By default, all exceptions thrown in activities are retryable. The activity will automatically retry with exponential backoff.Non-Retryable Exceptions
For exceptions that should not be retried (validation errors, etc.), implementNonRetryableExceptionContract:
src/Activity.php:127-129, when a NonRetryableExceptionContract is thrown:
- The exception is logged to the database
- The activity is immediately failed (no retries)
- The workflow execution stops
Activity Error Handling
Automatic Retries
Activities automatically retry on failure. Fromsrc/Activity.php:77-80, the default backoff schedule:
- 1st retry: after 1 second
- 2nd retry: after 2 seconds
- 3rd retry: after 5 seconds
- …
- 8th retry: after 120 seconds
- 9th+ retry: every 120 seconds (until max attempts)
Custom Backoff Strategy
Override thebackoff() method in your activity:
Exception Tracking
All exceptions are automatically logged to the database (fromsrc/Activity.php:121-125):
Activity Failed Hook
Thefailed() method is called when an activity fails permanently (from src/Activity.php:148-175):
Workflow Error Handling
Try-Catch in Workflows
Handle activity failures gracefully within workflows:Compensating Actions
Implement rollback logic for failures:Saga Pattern
Implement distributed transactions:Exception Job
When an activity fails permanently, anException job is dispatched (from src/Activity.php:167-174):
src/Exception.php:49-64):
- Attempts to resume the workflow
- Passes the exception to the workflow for handling
- Allows workflow code to catch and handle the exception
- Releases the job if workflow is still running
Error States
TransitionNotFound Exception
Thrown when workflow replay encounters an unexpected state (fromsrc/Exceptions/TransitionNotFound.php:9-26):
- Workflow code changed without versioning
- Database state is corrupted
- Race condition in workflow execution
VersionNotSupportedException
Thrown when workflow version is outside supported range:Monitoring and Alerting
Track Activity Failures
Dashboard Queries
Best Practices
Use Specific Exceptions
Use Specific Exceptions
Create meaningful exception types:
Add Context to Exceptions
Add Context to Exceptions
Implement Idempotency
Implement Idempotency
Make activities safe to retry:
Set Appropriate Timeouts
Set Appropriate Timeouts
Log Generously
Log Generously
Testing Error Scenarios
Related Topics
- Retries - Detailed retry configuration
- Middleware - Error handling middleware
- Activities - Activity basics
- Versioning - Safe code evolution