Introduction
Installation
Basic concepts
Getting Started
Entities
Actions
Events
Workflow
Error handling
Advanced usage
Examples
Error handling is an essential aspect of workflow management to ensure smooth and reliable execution. XWorkflows provides robust error handling mechanisms to detect and handle potential issues that may arise during workflow execution.
When executing an action on an entity, XWorkflows checks whether the entity can transition to the desired state based on the AllowedTransitions defined in the current state. If the entity is not in the appropriate state or the desired transition is not allowed, XWorkflows will throw an exception indicating an invalid state transition.
public enum WorkflowActionResultEnum
{
CanNotStartWorkflow,
TransitionNotAllowed,
Custom
}
// Execute an action that is not allowed in the current state
var invalidTransitionRequest = new InvalidTransitionActionRequest { /* Request data, if needed */ };
var (result, response) = await orderWorkflow.ExecuteAction(order, invalidTransitionRequest);
if(res.Errors.FirstOrDefault().Result == WorkflowActionResultEnum.CanNotStartWorkflow){
///handle error
}
In the event that an action's logic encounters an exception during execution, XWorkflows will catch the exception and propagate it back to the caller. To handle action execution failures, use standard exception handling techniques such as try-catch blocks.
try
{
// Execute an action that may encounter an exception during execution
var riskyActionRequest = new RiskyActionRequest { /* Request data, if needed */ };
var (result, response) = await orderWorkflow.ExecuteAction(order, riskyActionRequest);
}
catch (Exception ex)
{
// Handle the exception
Console.WriteLine($"Error: {ex.Message}");
}
XWorkflows provides meaningful error messages to help diagnose and resolve issues during workflow execution. The error messages will indicate the reason for the failure, such as an invalid state transition or the specific exception encountered during action execution.
For production applications, it is advisable to log errors and exceptions for debugging and monitoring purposes. You can use your preferred logging framework to log error details in case an exception is thrown during workflow execution.
XWorkflows ensures data integrity by following a transactional approach during workflow execution. If an exception occurs during action execution, any changes made to the entity state will be rolled back, leaving the entity in its original state.
To provide a better user experience, consider implementing graceful error handling in your application. When an exception occurs during workflow execution, you can inform the user about the error and provide guidance on resolving the issue.