Skip to content

Commit

Permalink
fix(api): return more info for error on worker set-version (#5813)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored May 18, 2021
1 parent 70909f2 commit 8303faa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
10 changes: 8 additions & 2 deletions engine/api/workflow/dao_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ovh/cds/engine/api/authentication"
"github.com/ovh/cds/engine/api/database/gorpmapping"
"github.com/ovh/cds/engine/api/services"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/telemetry"
)
Expand Down Expand Up @@ -84,7 +85,12 @@ func UpdateWorkflowRun(ctx context.Context, db gorp.SqlExecutor, wr *sdk.Workflo

runDB := Run(*wr)
if _, err := db.Update(&runDB); err != nil {
return sdk.WrapError(err, "Unable to update workflow run")
if e, ok := err.(*pq.Error); ok {
if e.Code == gorpmapper.ViolateUniqueKeyPGCode {
return sdk.NewError(sdk.ErrConflictData, e)
}
}
return sdk.WrapError(err, "unable to update workflow run")
}
wr.ID = runDB.ID
return nil
Expand Down Expand Up @@ -284,7 +290,7 @@ func LoadRunsIDsToDelete(db gorp.SqlExecutor, offset int64, limit int64) ([]int6
}

var ids []int64
querySelect := `SELECT id FROM workflow_run
querySelect := `SELECT id FROM workflow_run
WHERE to_delete = true
ORDER BY workflow_run.start ASC limit $1 offset $2`
_, err = db.Select(&ids, querySelect, limit, offset)
Expand Down
5 changes: 4 additions & 1 deletion engine/api/workflow_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,11 +1228,14 @@ func (api *API) postWorkflowJobSetVersionHandler() service.Handler {
}

if workflowRun.Version != nil && *workflowRun.Version != data.Value {
return sdk.NewErrorFrom(sdk.ErrForbidden, "cannot change existing workflow run version value")
return sdk.NewErrorFrom(sdk.ErrForbidden, "cannot change existing workflow run version value %q", *workflowRun.Version)
}

workflowRun.Version = &data.Value
if err := workflow.UpdateWorkflowRun(ctx, tx, workflowRun); err != nil {
if sdk.ErrorIs(err, sdk.ErrConflictData) {
return sdk.NewErrorFrom(err, "version %q already used by another workflow run", data.Value)
}
return err
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type WorkflowRunVersion struct {
func (w WorkflowRunVersion) IsValid() error {
_, err := semver.ParseTolerant(w.Value)
if err != nil {
return NewError(ErrWrongRequest, fmt.Errorf("value '%s' is not semver compatible: %v", w.Value, err))
return NewError(ErrWrongRequest, fmt.Errorf("value %q is not semver compatible: %v", w.Value, err))
}
return nil
}
Expand Down

0 comments on commit 8303faa

Please sign in to comment.