fix(agent-task/capi): return proper error message when resp is not a JSON

Signed-off-by: Babak K. Shandiz <babakks@github.com>
This commit is contained in:
Babak K. Shandiz 2025-09-22 12:59:51 +01:00
parent 002ba54683
commit 6fc5742a68
No known key found for this signature in database
GPG key ID: 9472CAEFF56C742E
2 changed files with 16 additions and 1 deletions

View file

@ -90,6 +90,11 @@ func (c *CAPIClient) CreateJob(ctx context.Context, owner, repo, problemStatemen
var j Job
if err := json.NewDecoder(res.Body).Decode(&j); err != nil {
if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { // accept 201 or 200
// This happens when there's an error like unauthorized (401).
statusText := fmt.Sprintf("%d %s", res.StatusCode, http.StatusText(res.StatusCode))
return nil, fmt.Errorf("failed to create job: %s", statusText)
}
return nil, fmt.Errorf("failed to decode create job response: %w", err)
}

View file

@ -330,7 +330,17 @@ func TestCreateJob(t *testing.T) {
wantErr: "failed to create job: 500 Internal Server Error",
},
{
name: "invalid JSON response",
name: "invalid JSON response, non-HTTP 200",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),
httpmock.StatusStringResponse(401, `Unauthorized`),
)
},
wantErr: "failed to create job: 401 Unauthorized",
},
{
name: "invalid JSON response, HTTP 200",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
httpmock.WithHost(httpmock.REST("POST", "agents/swe/v1/jobs/OWNER/REPO"), "api.githubcopilot.com"),