Skip to content

Avoid throwing System.Exception in FailTaskAsync #767

@ziagham

Description

@ziagham

What version of FlowSynx?

1.2.5

Describe the bug

File: src/FlowSynx.Infrastructure/Workflow/WorkflowTaskExecutor.cs
Method: FailTaskAsync

In the FailTaskAsync method, the current implementation throws a generic System.Exception:

private async Task FailTaskAsync(
    string userId,
    WorkflowTaskExecutionEntity entity,
    Exception ex,
    CancellationToken cancellationToken,
    string taskName)
{
    await CompleteTaskAsync(userId, entity, WorkflowTaskExecutionStatus.Failed, cancellationToken);
    _logger.LogError(ex, "Workflow task '{TaskName}' failed: {Message}", taskName, ex.Message);
    throw new Exception(ex.Message, ex);
}

Throwing System.Exception directly in user code is discouraged because it is too generic, making it harder for consumers to handle errors properly and understand the failure context.

Proposed Fix:

private async Task FailTaskAsync(
    string userId,
    WorkflowTaskExecutionEntity entity,
    Exception ex,
    CancellationToken cancellationToken,
    string taskName)
{
    await CompleteTaskAsync(userId, entity, WorkflowTaskExecutionStatus.Failed, cancellationToken);
    _logger.LogError(ex, "Workflow task '{TaskName}' failed: {Message}", taskName, ex.Message);
   throw new InvalidOperationException($"Workflow task '{taskName}' failed.", ex);
}

Benefits:

  • Provides a clear, meaningful exception type for consumers.
  • Improves maintainability and readability of exception handling.
  • Supports proper exception filtering in try/catch blocks.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions