Skip to content

Commit c12abc4

Browse files
committed
add raw api error test
1 parent 831b908 commit c12abc4

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

pkg/errors/error_test.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ func TestGitHubErrorContext(t *testing.T) {
6363
assert.Equal(t, "failed to execute mutation: GraphQL query failed", gqlError.Error())
6464
})
6565

66+
t.Run("Raw API errors can be added to context and retrieved", func(t *testing.T) {
67+
// Given a context with GitHub error tracking enabled
68+
ctx := ContextWithGitHubErrors(context.Background())
69+
70+
// Create a mock HTTP response
71+
resp := &http.Response{
72+
StatusCode: 404,
73+
Status: "404 Not Found",
74+
}
75+
originalErr := fmt.Errorf("raw content not found")
76+
77+
// When we add a raw API error to the context
78+
rawAPIErr := newGitHubRawAPIError("failed to fetch raw content", resp, originalErr)
79+
updatedCtx, err := addRawAPIErrorToContext(ctx, rawAPIErr)
80+
require.NoError(t, err)
81+
82+
// Then we should be able to retrieve the error from the updated context
83+
rawErrors, err := GetGitHubRawAPIErrors(updatedCtx)
84+
require.NoError(t, err)
85+
require.Len(t, rawErrors, 1)
86+
87+
rawError := rawErrors[0]
88+
assert.Equal(t, "failed to fetch raw content", rawError.Message)
89+
assert.Equal(t, resp, rawError.Response)
90+
assert.Equal(t, originalErr, rawError.Err)
91+
})
92+
6693
t.Run("multiple errors can be accumulated in context", func(t *testing.T) {
6794
// Given a context with GitHub error tracking enabled
6895
ctx := ContextWithGitHubErrors(context.Background())
@@ -82,6 +109,11 @@ func TestGitHubErrorContext(t *testing.T) {
82109
ctx, err = addGitHubGraphQLErrorToContext(ctx, gqlErr)
83110
require.NoError(t, err)
84111

112+
// And add a raw API error
113+
rawErr := newGitHubRawAPIError("raw error", &http.Response{StatusCode: 404}, fmt.Errorf("not found"))
114+
ctx, err = addRawAPIErrorToContext(ctx, rawErr)
115+
require.NoError(t, err)
116+
85117
// Then we should be able to retrieve all errors
86118
apiErrors, err := GetGitHubAPIErrors(ctx)
87119
require.NoError(t, err)
@@ -91,10 +123,15 @@ func TestGitHubErrorContext(t *testing.T) {
91123
require.NoError(t, err)
92124
assert.Len(t, gqlErrors, 1)
93125

126+
rawErrors, err := GetGitHubRawAPIErrors(ctx)
127+
require.NoError(t, err)
128+
assert.Len(t, rawErrors, 1)
129+
94130
// Verify error details
95131
assert.Equal(t, "first error", apiErrors[0].Message)
96132
assert.Equal(t, "second error", apiErrors[1].Message)
97133
assert.Equal(t, "graphql error", gqlErrors[0].Message)
134+
assert.Equal(t, "raw error", rawErrors[0].Message)
98135
})
99136

100137
t.Run("context pointer sharing allows middleware to inspect errors without context propagation", func(t *testing.T) {
@@ -160,6 +197,12 @@ func TestGitHubErrorContext(t *testing.T) {
160197
assert.Error(t, err)
161198
assert.Contains(t, err.Error(), "context does not contain GitHubCtxErrors")
162199
assert.Nil(t, gqlErrors)
200+
201+
// Same for raw API errors
202+
rawErrors, err := GetGitHubRawAPIErrors(ctx)
203+
assert.Error(t, err)
204+
assert.Contains(t, err.Error(), "context does not contain GitHubCtxErrors")
205+
assert.Nil(t, rawErrors)
163206
})
164207

165208
t.Run("ContextWithGitHubErrors resets existing errors", func(t *testing.T) {
@@ -169,18 +212,31 @@ func TestGitHubErrorContext(t *testing.T) {
169212
ctx, err := NewGitHubAPIErrorToCtx(ctx, "existing error", resp, fmt.Errorf("error"))
170213
require.NoError(t, err)
171214

172-
// Verify error exists
215+
// Add a raw API error too
216+
rawErr := newGitHubRawAPIError("existing raw error", &http.Response{StatusCode: 404}, fmt.Errorf("error"))
217+
ctx, err = addRawAPIErrorToContext(ctx, rawErr)
218+
require.NoError(t, err)
219+
220+
// Verify errors exist
173221
apiErrors, err := GetGitHubAPIErrors(ctx)
174222
require.NoError(t, err)
175223
assert.Len(t, apiErrors, 1)
176224

225+
rawErrors, err := GetGitHubRawAPIErrors(ctx)
226+
require.NoError(t, err)
227+
assert.Len(t, rawErrors, 1)
228+
177229
// When we call ContextWithGitHubErrors again
178230
resetCtx := ContextWithGitHubErrors(ctx)
179231

180-
// Then the errors should be cleared
232+
// Then all errors should be cleared
181233
apiErrors, err = GetGitHubAPIErrors(resetCtx)
182234
require.NoError(t, err)
183-
assert.Len(t, apiErrors, 0, "Errors should be reset")
235+
assert.Len(t, apiErrors, 0, "API errors should be reset")
236+
237+
rawErrors, err = GetGitHubRawAPIErrors(resetCtx)
238+
require.NoError(t, err)
239+
assert.Len(t, rawErrors, 0, "Raw API errors should be reset")
184240
})
185241

186242
t.Run("NewGitHubAPIErrorResponse creates MCP error result and stores context error", func(t *testing.T) {

0 commit comments

Comments
 (0)