-
Notifications
You must be signed in to change notification settings - Fork 1
Wrap all errors with context using fmt.Errorf #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
|
@copilot it would probably be helpful to add some context to errors. E.g. if you cannot find a task, which task are you looking for, if you cannot parse a file, which file cannot be parsed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR systematically improves error handling by wrapping all bare return err statements with contextual error messages using fmt.Errorf with the %w verb, aligning with the project's coding guidelines. Each wrapped error now includes actionable context about what operation failed and which resource was involved, while preserving the error chain for debugging.
Key changes:
- All 18 error return sites now include descriptive context about the failing operation
- Error messages identify specific resources (file paths, directory names, parameter values)
- Error chains are preserved using the
%wverb for proper error unwrapping
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/codingcontext/context.go | Added context to 8 error sites covering file I/O operations (stat, walk), markdown parsing (tasks, commands), and command execution |
| pkg/codingcontext/markdown/frontmatter.go | Distinguished between frontmatter structure unmarshaling and content map unmarshaling failures for task, command, and rule types (6 sites) |
| pkg/codingcontext/mcp/mcp.go | Added context to MCP server config unmarshaling errors, differentiating between structure and content map failures (2 sites) |
| pkg/codingcontext/taskparser/params.go | Included the failing parameter value in the parse error message for better debugging (1 site) |
| pkg/codingcontext/agent.go | Added context to agent value parsing error in the Set method (1 site) |
pkg/codingcontext/agent.go
Outdated
| agent, err := ParseAgent(value) | ||
| if err != nil { | ||
| return err | ||
| return fmt.Errorf("failed to set agent value: %w", err) |
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message could be more informative by including the attempted value, similar to how params.go does at line 380. Consider changing to: "failed to set agent value '%s': %w" with the value parameter included, so users can see which agent value caused the error without having to parse the wrapped error message.
| return fmt.Errorf("failed to set agent value: %w", err) | |
| return fmt.Errorf("failed to set agent value %q: %w", value, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied in commit d78cf0e. The error message now includes the agent value: fmt.Errorf("failed to set agent value %q: %w", value, err)
pkg/codingcontext/context.go
Outdated
| err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| return fmt.Errorf("error walking path %s: %w", path, err) |
Copilot
AI
Dec 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message pattern "error walking path" is inconsistent with the rest of the error messages in this file, which use "failed to [verb]" pattern (see lines 70, 97, 121, etc.). For consistency, consider using "failed to walk path %s: %w" instead.
| return fmt.Errorf("error walking path %s: %w", path, err) | |
| return fmt.Errorf("failed to walk path %s: %w", path, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied in commit d78cf0e. Changed to use consistent "failed to" pattern: fmt.Errorf("failed to walk path %s: %w", path, err)
|
@copilot apply changes based on the comments in this thread |
This has already been implemented in commit 711baa5. All errors now include relevant context:
All errors use %w to preserve error chains for debugging. |
… context Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Both suggestions applied in commit d78cf0e:
|
Error Handling Improvement - Complete ✅
All error handling has been improved to provide clear context and wrap underlying errors. Every instance of bare
return errhas been replaced with contextual error messages.Summary of Changes
Files Modified: 5
pkg/codingcontext/context.go- 8 error handling improvementspkg/codingcontext/agent.go- 1 error handling improvementpkg/codingcontext/markdown/frontmatter.go- 6 error handling improvements (+ fmt import)pkg/codingcontext/mcp/mcp.go- 2 error handling improvements (+ fmt import)pkg/codingcontext/taskparser/params.go- 1 error handling improvementLatest Updates (commit: applying code review feedback)
✅ agent.go:104 - Added agent value to error message for better debugging
"failed to set agent value: %w""failed to set agent value %q: %w", value, err✅ context.go:75 - Made error message pattern consistent with rest of file
"error walking path %s: %w""failed to walk path %s: %w"Error Handling Improvements
context.go
agent.go
frontmatter.go
mcp.go
params.go
Verification Results
return errremain in codebaseError Message Examples
Before:
return errAfter:
return fmt.Errorf("failed to parse task file %s: %w", path, err)This provides users with:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.