From ce27c9a279a0cfc5def7189a4607f6d515e73df7 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 28 Feb 2024 09:46:24 +0100 Subject: [PATCH] Respond with 500 in case of Incidents (#5004) * Modify error handling in Workflow middleware The handling for workflow faults in the Workflow Middleware has been altered. Previously, the code was checking if the workflow state status was designated as 'Faulted'. Now it checks if there are any incidents or problems in the workflow state, which provides a more comprehensive fault check. * Prevent deletion of all records with empty conditions Added checks in the 'Delete' and 'PagedDelete' methods in the 'Store.cs' file to prevent the deletion of all records if the conditions are empty. This will protect the system from unintentional data loss by stopping the execution of these methods when they have no parameters. --- src/modules/Elsa.Dapper/Services/Store.cs | 10 ++++++++++ .../Elsa.Http/Middleware/WorkflowsMiddleware.cs | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/modules/Elsa.Dapper/Services/Store.cs b/src/modules/Elsa.Dapper/Services/Store.cs index 0526cae05..5cc5ae2c3 100644 --- a/src/modules/Elsa.Dapper/Services/Store.cs +++ b/src/modules/Elsa.Dapper/Services/Store.cs @@ -245,6 +245,11 @@ public class Store where T : notnull { var query = _dbConnectionProvider.CreateQuery().Delete(TableName); filter(query); + + // If there are no conditions, we don't want to delete all records. + if (!query.Parameters.ParameterNames.Any()) + return 0; + using var connection = _dbConnectionProvider.GetConnection(); return await query.ExecuteAsync(connection); } @@ -262,6 +267,11 @@ public class Store where T : notnull { var selectQuery = _dbConnectionProvider.CreateQuery().From(TableName, primaryKey); filter(selectQuery); + + // If there are no conditions, we don't want to delete all records. + if (!selectQuery.Parameters.ParameterNames.Any()) + return 0; + selectQuery = selectQuery.OrderBy(orderFields.ToArray()).Page(pageArgs); var deleteQuery = _dbConnectionProvider.CreateQuery().Delete(TableName, primaryKey, selectQuery); diff --git a/src/modules/Elsa.Http/Middleware/WorkflowsMiddleware.cs b/src/modules/Elsa.Http/Middleware/WorkflowsMiddleware.cs index 9d6c8e0b2..f7e61323d 100644 --- a/src/modules/Elsa.Http/Middleware/WorkflowsMiddleware.cs +++ b/src/modules/Elsa.Http/Middleware/WorkflowsMiddleware.cs @@ -174,7 +174,7 @@ public class WorkflowsMiddleware cancellationTokens); // Check if there were any errors. - var faultedWorkflowState = affectedWorkflowStates.FirstOrDefault(x => x.SubStatus == WorkflowSubStatus.Faulted); + var faultedWorkflowState = affectedWorkflowStates.FirstOrDefault(x => x.Incidents.Any()); if (faultedWorkflowState != null) await HandleWorkflowFaultAsync(serviceProvider, httpContext, faultedWorkflowState, systemCancellationToken); @@ -291,9 +291,7 @@ public class WorkflowsMiddleware private async Task HandleWorkflowFaultAsync(IServiceProvider serviceProvider, IWorkflowRuntime workflowRuntime, HttpContext httpContext, WorkflowExecutionResult workflowExecutionResult, CancellationToken cancellationToken) { - var subStatus = workflowExecutionResult.SubStatus; - - if (subStatus != WorkflowSubStatus.Faulted || httpContext.Response.HasStarted) + if (!workflowExecutionResult.Incidents.Any() || httpContext.Response.HasStarted) return false; var workflowState = (await workflowRuntime.ExportWorkflowStateAsync(workflowExecutionResult.WorkflowInstanceId, cancellationToken))!;