Skip to content

Commit 6fc5742

Browse files
committed
fix(agent-task/capi): return proper error message when resp is not a JSON
Signed-off-by: Babak K. Shandiz <babakks@github.com>
1 parent 002ba54 commit 6fc5742

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

pkg/cmd/agent-task/capi/job.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func (c *CAPIClient) CreateJob(ctx context.Context, owner, repo, problemStatemen
9090

9191
var j Job
9292
if err := json.NewDecoder(res.Body).Decode(&j); err != nil {
93+
if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { // accept 201 or 200
94+
// This happens when there's an error like unauthorized (401).
95+
statusText := fmt.Sprintf("%d %s", res.StatusCode, http.StatusText(res.StatusCode))
96+
return nil, fmt.Errorf("failed to create job: %s", statusText)
97+
}
9398
return nil, fmt.Errorf("failed to decode create job response: %w", err)
9499
}
95100

pkg/cmd/agent-task/capi/job_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,17 @@ func TestCreateJob(t *testing.T) {
330330
wantErr: "failed to create job: 500 Internal Server Error",
331331
},
332332
{
333-
name: "invalid JSON response",
333+
name: "invalid JSON response, non-HTTP 200",
334+
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
335+
reg.Register(
336+
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
337+
httpmock.StatusStringResponse(401, `Unauthorized`),
338+
)
339+
},
340+
wantErr: "failed to create job: 401 Unauthorized",
341+
},
342+
{
343+
name: "invalid JSON response, HTTP 200",
334344
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
335345
reg.Register(
336346
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),

0 commit comments

Comments
 (0)