Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 23, 2025

Error Handling Improvement - Complete ✅

All error handling has been improved to provide clear context and wrap underlying errors. Every instance of bare return err has been replaced with contextual error messages.

Summary of Changes

Files Modified: 5

  • pkg/codingcontext/context.go - 8 error handling improvements
  • pkg/codingcontext/agent.go - 1 error handling improvement
  • pkg/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 improvement

Latest Updates (commit: applying code review feedback)

agent.go:104 - Added agent value to error message for better debugging

  • Changed: "failed to set agent value: %w"
  • To: "failed to set agent value %q: %w", value, err
  • Now shows which agent value failed to parse

context.go:75 - Made error message pattern consistent with rest of file

  • Changed: "error walking path %s: %w"
  • To: "failed to walk path %s: %w"
  • All error messages now use "failed to [verb]" pattern

Error Handling Improvements

context.go

  • ✅ Line 70: os.Stat error now includes directory path
  • ✅ Line 75: filepath.Walk callback error now uses consistent "failed to" pattern and includes path
  • ✅ Line 97: filepath.Walk error now includes directory path
  • ✅ Line 121: ParseMarkdownFile error now includes file path
  • ✅ Line 135: ParseAgent error now includes context about task frontmatter
  • ✅ Line 155: ParseTask error now includes context about task content parsing
  • ✅ Line 177: findCommand error now includes command name
  • ✅ Line 221: ParseMarkdownFile error in findCommand now includes file path

agent.go

  • ✅ Line 104: ParseAgent error in Set method now includes the failing agent value

frontmatter.go

  • ✅ Lines 60, 65: JSON unmarshal errors in TaskFrontMatter now distinguish between frontmatter and content
  • ✅ Lines 97, 102: JSON unmarshal errors in CommandFrontMatter now distinguish between frontmatter and content
  • ✅ Lines 146, 151: JSON unmarshal errors in RuleFrontMatter now distinguish between frontmatter and content

mcp.go

  • ✅ Lines 61, 71: JSON unmarshal errors in MCPServerConfig now distinguish between config and content

params.go

  • ✅ Line 380: ParseParams error now includes the parameter value that failed to parse

Verification Results

  • Build: Successfully compiles with no errors
  • Tests: All tests pass (100% pass rate)
  • Linter: No linting issues detected
  • Code Review: Applied all review feedback
  • Verification: Zero instances of bare return err remain in codebase

Error Message Examples

Before: return err
After: return fmt.Errorf("failed to parse task file %s: %w", path, err)

This provides users with:

  1. Clear context about what operation failed
  2. Specific details (file paths, command names, agent values)
  3. Properly wrapped underlying errors for debugging
  4. Consistent error message patterns throughout the codebase
Original prompt

Review the error handling. Every error should give clear context for the error and wrap any underlying error. Never just return err.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve error handling by providing clear context Wrap all errors with context using fmt.Errorf Dec 24, 2025
Copilot AI requested a review from alexec December 24, 2025 00:05
@alexec alexec marked this pull request as ready for review December 24, 2025 00:06
Copilot AI review requested due to automatic review settings December 24, 2025 00:06
@alexec
Copy link
Contributor

alexec commented Dec 24, 2025

@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.

Copy link
Contributor

Copilot AI left a 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 %w verb 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)

agent, err := ParseAgent(value)
if err != nil {
return err
return fmt.Errorf("failed to set agent value: %w", err)
Copy link

Copilot AI Dec 24, 2025

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.

Suggested change
return fmt.Errorf("failed to set agent value: %w", err)
return fmt.Errorf("failed to set agent value %q: %w", value, err)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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)

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)
Copy link

Copilot AI Dec 24, 2025

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.

Suggested change
return fmt.Errorf("error walking path %s: %w", path, err)
return fmt.Errorf("failed to walk path %s: %w", path, err)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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)

@alexec
Copy link
Contributor

alexec commented Dec 24, 2025

@copilot apply changes based on the comments in this thread

Copy link
Contributor Author

Copilot AI commented Dec 24, 2025

@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.

This has already been implemented in commit 711baa5. All errors now include relevant context:

  • Task errors include file paths and task names (e.g., "failed to parse task file %s", "task not found: %s")
  • Command errors include command names and file paths (e.g., "failed to find command %s", "command not found: %s")
  • File operation errors include directory/file paths (e.g., "failed to stat directory %s", "failed to walk directory %s")
  • Parameter errors include the failing value (e.g., "failed to parse parameter '%s'")

All errors use %w to preserve error chains for debugging.

… context

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 24, 2025

@copilot apply changes based on the comments in this thread

Both suggestions applied in commit d78cf0e:

  1. agent.go now includes the agent value in the error message
  2. context.go now uses consistent "failed to" pattern for all error messages

@alexec alexec enabled auto-merge (squash) December 24, 2025 00:14
@alexec alexec merged commit 51b6a24 into main Dec 24, 2025
1 check passed
@alexec alexec deleted the copilot/improve-error-handling-logic branch December 24, 2025 00:15
Copilot AI requested a review from alexec December 24, 2025 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants