OnError

@OnMessage(functionKind = FunctionKind.ERROR, defaultFunctionName = "onError")
annotation class OnError

Marks a function as a command error handler within an aggregate.

Functions annotated with @OnError are invoked when command processing fails. They provide an opportunity to handle errors gracefully, potentially recovering from failures or publishing error events for monitoring and debugging.

Error handlers can:

  • Log error details for monitoring

  • Publish error events for external systems

  • Attempt error recovery or compensation

  • Transform exceptions into domain events

  • Trigger fallback business logic

Error handlers receive the original command and the exception that occurred, allowing for context-aware error processing.

Example usage:

@AggregateRoot
class OrderAggregate {

@OnCommand
fun create(command: CreateOrderCommand): OrderCreated {
// Command processing that might fail
validateOrder(command)
return OrderCreated(command.orderId, command.items)
}

@OnError
fun onCreateError(command: CreateOrderCommand, error: Exception): OrderCreationFailed? {
// Log the error
logger.error("Failed to create order ${command.orderId}", error)

// Publish error event for monitoring
return if (error is ValidationException) {
OrderCreationFailed(command.orderId, error.message ?: "Validation failed")
} else null // Don't publish events for unexpected errors
}
}

See also

for regular command handlers

CommandAggregate

for command processing implementation