Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pkg/workflow/error_aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ package workflow

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testFormattedErrorChainType struct {
message string
}

func (e *testFormattedErrorChainType) Error() string {
return e.message
}

func TestNewErrorCollector(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -271,3 +280,21 @@ func TestErrorCollectorFormattedError(t *testing.T) {
})
}
}

func TestErrorCollectorFormattedError_PreservesErrorChain(t *testing.T) {
collector := NewErrorCollector(false)
sentinelErr := errors.New("sentinel")
typedErr := &testFormattedErrorChainType{message: "typed"}

require.NoError(t, collector.Add(fmt.Errorf("wrapped sentinel: %w", sentinelErr)), "Should collect wrapped sentinel error")
require.NoError(t, collector.Add(fmt.Errorf("wrapped typed: %w", typedErr)), "Should collect wrapped typed error")

result := collector.FormattedError("validation")
require.Error(t, result, "Should return formatted error")

require.ErrorIs(t, result, sentinelErr, "FormattedError should preserve errors.Is chain")

var extractedTypedErr *testFormattedErrorChainType
require.ErrorAs(t, result, &extractedTypedErr, "FormattedError should preserve errors.As chain")
assert.Equal(t, typedErr, extractedTypedErr, "errors.As should extract the wrapped typed error")
}
11 changes: 2 additions & 9 deletions pkg/workflow/workflow_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,8 @@ func (c *ErrorCollector) FormattedError(category string) error {
return c.errors[0]
}

// Build formatted error with count header
var sb strings.Builder
fmt.Fprintf(&sb, "Found %d %s errors:", len(c.errors), category)
for _, err := range c.errors {
sb.WriteString("\n • ")
sb.WriteString(err.Error())
}

return fmt.Errorf("%s", sb.String())
header := fmt.Sprintf("Found %d %s errors:", len(c.errors), category)
return fmt.Errorf("%s\n%w", header, errors.Join(c.errors...))
}

var sharedWorkflowLog = logger.New("workflow:shared_workflow_error")
Expand Down
Loading