From 21ccabc832f1c8e2ea88e7fb9678815e97dc330a Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Wed, 3 Sep 2025 13:01:40 -0600 Subject: [PATCH] Simplify error handling in CreateJob response Refactored the CreateJob method to decode the response body into the Job struct before checking for error status codes. Error messages are now extracted directly from the Job struct, removing redundant error parsing logic. --- pkg/cmd/agent-task/capi/job.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/agent-task/capi/job.go b/pkg/cmd/agent-task/capi/job.go index ec215c5fe..03eaa376d 100644 --- a/pkg/cmd/agent-task/capi/job.go +++ b/pkg/cmd/agent-task/capi/job.go @@ -79,24 +79,16 @@ func (c *CAPIClient) CreateJob(ctx context.Context, owner, repo, problemStatemen return nil, err } defer res.Body.Close() - if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { // accept 201 or 200 - // Attempt to parse error body for message - var er struct { - Error struct { - Message string `json:"message"` - } `json:"error"` - } - _ = json.NewDecoder(res.Body).Decode(&er) - msg := er.Error.Message - if msg == "" { - msg = res.Status - } - return nil, fmt.Errorf("failed to create job: %s", msg) - } + var j Job if err := json.NewDecoder(res.Body).Decode(&j); err != nil { return nil, fmt.Errorf("failed to decode create job response: %w", err) } + + if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusOK { // accept 201 or 200 + return nil, fmt.Errorf("failed to create job: %s", j.ErrorInfo.Message) + } + return &j, nil }