Error Handling
Error handling implements robust recovery patterns without introducing new primitives beyond Nodes and Events.
Core Principles
Happen's approach to error handling is built on these fundamental principles:
Errors as Flow Branches: Errors are treated as natural branches in the event flow
Functional Error Handling: Error handlers are just functions that can be returned
Context for Error Information: Error details flow through context between functions
Causal Error Tracking: Errors maintain causal relationships like normal events
Decentralized Recovery: Nodes make local recovery decisions when possible
This approach achieves remarkable power with minimal complexity by leveraging existing primitives rather than introducing special error-handling constructs.
The Functional Error Model
In Happen, error handling is fully integrated into the Event Continuum through function returns:
// Register an event handler
orderNode.on("process-order", function validateOrder(event, context) {
// Validate the order
const validation = validateOrderData(event.payload);
if (!validation.valid) {
// Return error handler function on validation failure
return handleInvalidOrder;
}
// Proceed with valid order
context.validated = true;
return processOrder;
});
// Error handler function
function handleInvalidOrder(event, context) {
// Log the validation failure
logValidationFailure(event.payload);
// Return error result
return {
success: false,
reason: "validation-failed",
details: "Invalid order data"
};
}This functional approach means:
Errors are just another branch in the flow
Error handlers are regular functions
Error information flows through context
No special syntax or constructs are needed
Error Propagation and Context
Error information naturally flows through context:
This approach allows error information to flow naturally through the event chain, maintaining complete context for diagnostics and recovery.
Recovery Patterns
Happen's functional approach enables powerful recovery patterns through composition.
Retry Pattern
Circuit Breaker Pattern
Fallback Pattern
Error Events as First-Class Citizens
In Happen, errors can also be treated as normal events, enabling system-wide error handling:
Distributed Error Handling
When errors cross node boundaries, they naturally maintain their causal context:
Supervisor Pattern
For system-wide resilience, you can create supervisor nodes that monitor and manage error recovery:
Composing Error Handling with Normal Flow
Error handling in Happen integrates seamlessly with normal event processing:
Treating errors as branches in the functional flow provides sophisticated error handling capabilities without introducing special constructs.
Key takeaways:
Use Function Returns for Error Flow: Return error handler functions to handle errors
Leverage Context: Store error information in the context object for diagnostics
Implement Recovery Patterns: Build error recovery using function composition
Apply Resilience Patterns: Implement retry, circuit breakers, and fallbacks as needed
Decentralized Recovery: Let nodes make local decisions about error handling
Last updated