Merge pull request #11783 from cli/babakks/display-proper-error-in-agent-task-create

`gh agent-task create`: return proper error message when resp is not a JSON
This commit is contained in:
Babak K. Shandiz 2025-09-22 15:04:47 +01:00 committed by GitHub
commit ecb236b2dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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"),