Skip to content

Commit

Permalink
decode response only if status is ok (#101)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin authored Jan 15, 2025
1 parent 99cced7 commit 8e2b1b3
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,16 @@ func (r *BuildClient) Build(
_ = resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return k6build.Artifact{}, k6build.NewWrappedError(api.ErrRequestFailed, errors.New(resp.Status))
}

buildResponse := api.BuildResponse{}
err = json.NewDecoder(resp.Body).Decode(&buildResponse)
if err != nil {
return k6build.Artifact{}, k6build.NewWrappedError(api.ErrRequestFailed, err)
}

if resp.StatusCode != http.StatusOK {
// if an error occurred (e.g. internal server error) the Error may not be available
if buildResponse.Error == nil {
return k6build.Artifact{}, k6build.NewWrappedError(api.ErrRequestFailed, errors.New(resp.Status))
}

// we expect the Error to have an api.Error we we don't wrap it again
return k6build.Artifact{}, buildResponse.Error
}

if buildResponse.Error != nil {
return k6build.Artifact{}, buildResponse.Error
}
Expand Down

0 comments on commit 8e2b1b3

Please sign in to comment.