Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
udsamani committed Sep 11, 2024
1 parent 6b8d0b3 commit c7a7dde
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/jobstore/boltdb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ func (b *BoltJobStore) CreateJob(ctx context.Context, job models.Job) error {
job.Normalize()
err := job.Validate()
if err != nil {
return err
return jobstore.NewJobStoreError(err.Error())
}
return b.update(ctx, func(tx *bolt.Tx) (err error) {
return b.createJob(tx, job)
Expand Down
4 changes: 2 additions & 2 deletions pkg/orchestrator/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ func (e *BaseEndpoint) StopJob(ctx context.Context, request *StopJobRequest) (St
// no need to stop a job that is already stopped
return StopJobResponse{}, nil
case models.JobStateTypeCompleted:
return StopJobResponse{}, fmt.Errorf("cannot stop job in state %s", job.State.StateType)
return StopJobResponse{}, models.NewBaseError("cannot stop job in state %s", job.State.StateType)
default:
// continue
}

txContext, err := e.store.BeginTx(ctx)
if err != nil {
return StopJobResponse{}, fmt.Errorf("failed to begin transaction: %w", err)
return StopJobResponse{}, jobstore.NewBoltDbError(err.Error())
}

defer func() {
Expand Down
29 changes: 24 additions & 5 deletions pkg/publicapi/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (c *httpClient) Get(ctx context.Context, endpoint string, in apimodels.GetR
r := in.ToHTTPRequest()

_, resp, err := c.doRequest(ctx, http.MethodGet, endpoint, r) //nolint:bodyclose // this is being closed
if err != nil {
return err
}

if resp.StatusCode == http.StatusUnauthorized {
return apimodels.NewUnauthorizedError("invalid token")
Expand Down Expand Up @@ -93,20 +96,36 @@ func (c *httpClient) write(ctx context.Context, verb, endpoint string, in apimod
if r.BodyObj == nil && r.Body == nil {
r.BodyObj = in
}
_, resp, err := requireOK(c.doRequest(ctx, verb, endpoint, r)) //nolint:bodyclose // this is being closed
if err != nil && resp != nil && resp.StatusCode == http.StatusUnauthorized {
return apimodels.ErrInvalidToken
} else if err != nil {

_, resp, err := c.doRequest(ctx, verb, endpoint, r) //nolint:bodyclose // this is being closed
defer resp.Body.Close()
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusUnauthorized {
return apimodels.ErrInvalidToken
}

var apiError *apimodels.APIError
if resp.StatusCode != http.StatusOK {
apiError, err = apimodels.FromHttpResponse(resp)
if err != nil {
return err
}
}

if apiError != nil {
return apiError
}

if out != nil {
if err := decodeBody(resp, &out); err != nil {
return err
}
out.Normalize()
}

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/publicapi/middleware/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ func CustomHTTPErrorHandler(err error, c echo.Context) {
code = e.Code().HTTPStatusCode()
message = e.Error()

case *echo.HTTPError:
// This is needed, in case any other middleware throws an error. In
// such a scenario we just use it as the error code.
code = e.Code
message = e.Message.(string)

default:
// In an ideal world this should never happen. We should always have are errors
// from server as APIError. If output is this generic string, one should evaluate
// and map it to APIError and send in appropriate message.= http.StatusInternalServerError
message = "internal server error"
code = c.Response().Status
}

requestID := c.Request().Header.Get(echo.HeaderXRequestID)
Expand Down

0 comments on commit c7a7dde

Please sign in to comment.