Skip to content

Commit 0f3f1ea

Browse files
authored
fix(errors): replace fmt.Errorf("%s") with errors.New and preserve error chains in pkg/workflow + pkg/parser (#29197)
1 parent 2f845b1 commit 0f3f1ea

10 files changed

Lines changed: 18 additions & 11 deletions

pkg/parser/frontmatter_content.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func ExtractFrontmatterFromContent(content string) (*FrontmatterResult, error) {
9999
// Use FormatYAMLError to provide source-positioned error output with adjusted line numbers
100100
// FrontmatterStart is 2 (line 2 is where frontmatter content starts after opening ---)
101101
formattedErr := FormatYAMLError(err, 2, frontmatterYAML)
102-
return nil, fmt.Errorf("failed to parse frontmatter:\n%s", formattedErr)
102+
return nil, &FormattedParserError{formatted: "failed to parse frontmatter:\n" + formattedErr, cause: err}
103103
}
104104

105105
// Ensure frontmatter is never nil (yaml.Unmarshal sets it to nil for empty YAML)

pkg/parser/import_error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func FormatImportCycleError(err *ImportCycleError) error {
6565
messageBuilder.WriteString("2. Remove one of the imports to break the cycle\n")
6666
messageBuilder.WriteString("3. Consider restructuring your workflow imports to avoid circular dependencies\n")
6767

68-
return fmt.Errorf("%s", messageBuilder.String())
68+
return &FormattedParserError{formatted: messageBuilder.String(), cause: err}
6969
}
7070

7171
// FormattedParserError is a sentinel error type returned by FormatImportError (and similar

pkg/workflow/dangerous_permissions_validation.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workflow
22

33
import (
4+
"errors"
45
"fmt"
56
"strings"
67
)
@@ -93,5 +94,5 @@ func formatDangerousPermissionsError(writePermissions []PermissionScope) error {
9394
lines = append(lines, fmt.Sprintf(" %s: read", scope))
9495
}
9596

96-
return fmt.Errorf("%s", strings.Join(lines, "\n"))
97+
return errors.New(strings.Join(lines, "\n"))
9798
}

pkg/workflow/engine_definition.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package workflow
2929

3030
import (
31+
"errors"
3132
"fmt"
3233
"sort"
3334
"strings"
@@ -277,5 +278,5 @@ func (c *EngineCatalog) Resolve(id string, config *EngineConfig) (*ResolvedEngin
277278
constants.DocsEnginesURL)
278279
}
279280

280-
return nil, fmt.Errorf("%s", errMsg)
281+
return nil, errors.New(errMsg)
281282
}

pkg/workflow/engine_validation.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ package workflow
3535

3636
import (
3737
"encoding/json"
38+
"errors"
3839
"fmt"
3940
"os"
4041
"path/filepath"
@@ -139,7 +140,7 @@ func (c *Compiler) validateEngineInlineDefinition(config *EngineConfig) error {
139140
errMsg = fmt.Sprintf("inline engine definition references unknown runtime.id: %s. Known runtime IDs are: %s.\n\nDid you mean: %s?\n\nExample:\nengine:\n runtime:\n id: codex\n\nSee: %s",
140141
config.ID, enginesStr, suggestions[0], constants.DocsEnginesURL)
141142
}
142-
return fmt.Errorf("%s", errMsg)
143+
return errors.New(errMsg)
143144
}
144145
}
145146

pkg/workflow/permissions_validation.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package workflow
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"maps"
78
"slices"
@@ -351,7 +352,7 @@ func (c *Compiler) ValidateIncludedPermissions(topPermissionsYAML string, import
351352
fmt.Fprintf(&errorMsg, " %s: %s\n", scope, level)
352353
}
353354

354-
return fmt.Errorf("%s", errorMsg.String())
355+
return errors.New(errorMsg.String())
355356
}
356357

357358
permissionsValidationLog.Print("All included workflow permissions are satisfied by main workflow")

pkg/workflow/runtime_validation.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
package workflow
4141

4242
import (
43+
"errors"
4344
"fmt"
4445
"os"
4546
"strings"
@@ -88,7 +89,7 @@ func (c *Compiler) validateExpressionSizes(yamlContent string) error {
8889
lineNum+1, actualSize, maxSizeFormatted)
8990
}
9091

91-
return fmt.Errorf("%s", errorMsg)
92+
return errors.New(errorMsg)
9293
}
9394
}
9495

pkg/workflow/safe_update_enforcement.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package workflow
22

33
import (
4-
"fmt"
4+
"errors"
55
"sort"
66
"strings"
77

@@ -254,7 +254,7 @@ func buildSafeUpdateError(secretViolations, addedActions, removedActions []strin
254254
}
255255

256256
sb.WriteString("\n\nRemediation options:\n 1. Use the --approve flag to allow the changes.\n 2. Revert the unapproved changes.\n 3. Use an interactive coding agent to review and approve the changes.")
257-
return fmt.Errorf("%s", sb.String())
257+
return errors.New(sb.String())
258258
}
259259

260260
// buildSafeUpdateWarningPrompt wraps the raw safe update violation message in a

pkg/workflow/template_injection_utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workflow
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
"sort"
@@ -226,5 +227,5 @@ func formatTemplateInjectionError(violations []TemplateInjectionViolation) error
226227
builder.WriteString(" - https://docs.zizmor.sh/audits/#template-injection\n")
227228
builder.WriteString(" - scratchpad/template-injection-prevention.md\n")
228229

229-
return fmt.Errorf("%s", builder.String())
230+
return errors.New(builder.String())
230231
}

pkg/workflow/tools_validation_github_toolsets.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workflow
22

33
import (
4+
"errors"
45
"fmt"
56
"sort"
67
"strings"
@@ -94,7 +95,7 @@ func validateGitHubToolsAgainstToolsetsImpl(allowedTools []string, enabledToolse
9495
errMsg.WriteString(fmt.Sprintf("Valid GitHub tools include: %s\n\n", formatList(validTools[:exampleCount])))
9596
errMsg.WriteString("See all tools: https://github.com/github/gh-aw/blob/main/pkg/workflow/data/github_tool_to_toolset.json")
9697

97-
return fmt.Errorf("%s", errMsg.String())
98+
return errors.New(errMsg.String())
9899
}
99100

100101
if len(missingToolsets) > 0 {

0 commit comments

Comments
 (0)