Skip to content

Commit 51b6a24

Browse files
Copilotalexec
andauthored
Wrap all errors with context using fmt.Errorf (#170)
* Initial plan * Improve error handling with clear context and wrapped errors Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> * Apply code review suggestions: improve error messages consistency and context Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
1 parent ee2a9b8 commit 51b6a24

5 files changed

Lines changed: 23 additions & 19 deletions

File tree

pkg/codingcontext/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var agentPathPatterns = map[Agent][]string{
101101
func (a *Agent) Set(value string) error {
102102
agent, err := ParseAgent(value)
103103
if err != nil {
104-
return err
104+
return fmt.Errorf("failed to set agent value %q: %w", value, err)
105105
}
106106

107107
*a = agent

pkg/codingcontext/context.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func (cc *Context) visitMarkdownFiles(searchDirFn func(path string) []string, vi
6767
if _, err := os.Stat(dir); os.IsNotExist(err) {
6868
continue
6969
} else if err != nil {
70-
return err
70+
return fmt.Errorf("failed to stat directory %s: %w", dir, err)
7171
}
7272

7373
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
7474
if err != nil {
75-
return err
75+
return fmt.Errorf("failed to walk path %s: %w", path, err)
7676
}
7777
ext := filepath.Ext(path) // .md or .mdc
7878
if info.IsDir() || ext != ".md" && ext != ".mdc" {
@@ -94,7 +94,7 @@ func (cc *Context) visitMarkdownFiles(searchDirFn func(path string) []string, vi
9494
return visitor(path)
9595
})
9696
if err != nil {
97-
return err
97+
return fmt.Errorf("failed to walk directory %s: %w", dir, err)
9898
}
9999
}
100100

@@ -118,7 +118,7 @@ func (cc *Context) findTask(taskName string) error {
118118
var frontMatter markdown.TaskFrontMatter
119119
md, err := markdown.ParseMarkdownFile(path, &frontMatter)
120120
if err != nil {
121-
return err
121+
return fmt.Errorf("failed to parse task file %s: %w", path, err)
122122
}
123123

124124
// Extract selector labels from task frontmatter and add them to cc.includes.
@@ -132,7 +132,7 @@ func (cc *Context) findTask(taskName string) error {
132132
if frontMatter.Agent != "" {
133133
agent, err := ParseAgent(frontMatter.Agent)
134134
if err != nil {
135-
return err
135+
return fmt.Errorf("failed to parse agent from task frontmatter: %w", err)
136136
}
137137
cc.agent = agent
138138
}
@@ -152,7 +152,7 @@ func (cc *Context) findTask(taskName string) error {
152152
// Parse the task content (including user_prompt) to separate text blocks from slash commands
153153
task, err := taskparser.ParseTask(taskContent)
154154
if err != nil {
155-
return err
155+
return fmt.Errorf("failed to parse task content: %w", err)
156156
}
157157

158158
// Build the final content by processing each block
@@ -174,7 +174,7 @@ func (cc *Context) findTask(taskName string) error {
174174
} else if block.SlashCommand != nil {
175175
commandContent, err := cc.findCommand(block.SlashCommand.Name, block.SlashCommand.Params())
176176
if err != nil {
177-
return err
177+
return fmt.Errorf("failed to find command %s: %w", block.SlashCommand.Name, err)
178178
}
179179
finalContent.WriteString(commandContent)
180180
}
@@ -218,7 +218,7 @@ func (cc *Context) findCommand(commandName string, params taskparser.Params) (st
218218
var frontMatter markdown.CommandFrontMatter
219219
md, err := markdown.ParseMarkdownFile(path, &frontMatter)
220220
if err != nil {
221-
return err
221+
return fmt.Errorf("failed to parse command file %s: %w", path, err)
222222
}
223223

224224
// Extract selector labels from command frontmatter and add them to cc.includes.

pkg/codingcontext/markdown/frontmatter.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package markdown
22

33
import (
44
"encoding/json"
5+
"fmt"
56

67
"github.com/kitproj/coding-context-cli/pkg/codingcontext/mcp"
78
)
@@ -57,12 +58,12 @@ func (t *TaskFrontMatter) UnmarshalJSON(data []byte) error {
5758
}
5859

5960
if err := json.Unmarshal(data, aux); err != nil {
60-
return err
61+
return fmt.Errorf("failed to unmarshal task frontmatter: %w", err)
6162
}
6263

6364
// Also unmarshal into Content map
6465
if err := json.Unmarshal(data, &t.BaseFrontMatter.Content); err != nil {
65-
return err
66+
return fmt.Errorf("failed to unmarshal task frontmatter content: %w", err)
6667
}
6768

6869
return nil
@@ -94,12 +95,12 @@ func (c *CommandFrontMatter) UnmarshalJSON(data []byte) error {
9495
}
9596

9697
if err := json.Unmarshal(data, aux); err != nil {
97-
return err
98+
return fmt.Errorf("failed to unmarshal command frontmatter: %w", err)
9899
}
99100

100101
// Also unmarshal into Content map
101102
if err := json.Unmarshal(data, &c.BaseFrontMatter.Content); err != nil {
102-
return err
103+
return fmt.Errorf("failed to unmarshal command frontmatter content: %w", err)
103104
}
104105

105106
return nil
@@ -143,12 +144,12 @@ func (r *RuleFrontMatter) UnmarshalJSON(data []byte) error {
143144
}
144145

145146
if err := json.Unmarshal(data, aux); err != nil {
146-
return err
147+
return fmt.Errorf("failed to unmarshal rule frontmatter: %w", err)
147148
}
148149

149150
// Also unmarshal into Content map
150151
if err := json.Unmarshal(data, &r.BaseFrontMatter.Content); err != nil {
151-
return err
152+
return fmt.Errorf("failed to unmarshal rule frontmatter content: %w", err)
152153
}
153154

154155
return nil

pkg/codingcontext/mcp/mcp.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package mcp
22

3-
import "encoding/json"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
47

58
// TransportType defines the communication protocol used by the server.
69
// Supported by both Claude and Cursor.
@@ -58,7 +61,7 @@ func (m *MCPServerConfig) UnmarshalJSON(data []byte) error {
5861
}
5962

6063
if err := json.Unmarshal(data, aux); err != nil {
61-
return err
64+
return fmt.Errorf("failed to unmarshal MCP server config: %w", err)
6265
}
6366

6467
// Initialize Content map if needed
@@ -68,7 +71,7 @@ func (m *MCPServerConfig) UnmarshalJSON(data []byte) error {
6871

6972
// Also unmarshal into Content map
7073
if err := json.Unmarshal(data, &m.Content); err != nil {
71-
return err
74+
return fmt.Errorf("failed to unmarshal MCP server config content: %w", err)
7275
}
7376

7477
return nil

pkg/codingcontext/taskparser/params.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (p Params) Set(value string) error {
377377

378378
params, err := ParseParams(quotedValue)
379379
if err != nil {
380-
return err
380+
return fmt.Errorf("failed to parse parameter '%s': %w", value, err)
381381
}
382382

383383
maps.Copy(p, params)

0 commit comments

Comments
 (0)