Skip to content

Commit

Permalink
feat(api,ui): improve error and display request id in ui (#5225)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Jun 4, 2020
1 parent dc2af41 commit 02c2eab
Show file tree
Hide file tree
Showing 65 changed files with 479 additions and 297 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ UI_DIST = ui/ui.tar.gz
FILES = dist/FILES

TARGET_DIR := dist/
ALL_DIST = $(ENGINE_DIST)
ALL_DIST := $(ALL_DIST) $(WORKER_DIST)
ALL_DIST := $(ALL_DIST) $(CLI_DIST)
ALL_DIST = $(ENGINE_DIST)
ALL_DIST := $(ALL_DIST) $(WORKER_DIST)
ALL_DIST := $(ALL_DIST) $(CLI_DIST)
ALL_DIST := $(ALL_DIST) $(UI_DIST)
ALL_DIST := $(ALL_DIST) $(CONTRIB_DIST)
ALL_TARGETS := $(foreach DIST,$(ALL_DIST),$(addprefix $(TARGET_DIR),$(notdir $(DIST))))
Expand All @@ -49,7 +49,7 @@ goinstall:

build:
$(info Building CDS Components for $(TARGET_OS) - $(TARGET_ARCH))
$(MAKE) build_ui
$(MAKE) build_ui -j1
$(MAKE) build_engine -j4
$(MAKE) build_worker -j4
$(MAKE) build_cli -j4
Expand Down Expand Up @@ -85,7 +85,7 @@ dist: $(ALL_TARGETS)
rm -f $(FILES)
cd dist/ && for i in `ls -p | grep -v /|grep -v FILES`; do echo "$$i;`${SHA512} $$i|cut -d ' ' -f1`" >> FILES; done;

clean:
clean:
@rm -rf target
@rm -rf dist
$(MAKE) clean -C engine
Expand All @@ -95,7 +95,7 @@ clean:
$(MAKE) clean -C contrib

deb: dist target/cds-engine.deb

$(TARGET_DIR)/config.toml.sample:
$(TARGET_DIR)/cds-engine-linux-amd64 config new > $(TARGET_DIR)/config.toml.sample

Expand All @@ -109,4 +109,4 @@ tar: target/cds-engine.tar.gz

target/cds-engine.tar.gz: $(TARGET_DIR)/config.toml.sample $(TARGET_DIR)/tmpl-config
mkdir -p target
tar -czvf target/cds-engine.tar.gz -C $(TARGET_DIR) .
tar -czvf target/cds-engine.tar.gz -C $(TARGET_DIR) .
7 changes: 3 additions & 4 deletions engine/api/application/application_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package application

import (
"context"
"fmt"
"strings"

"github.com/go-gorp/gorp"
Expand Down Expand Up @@ -83,7 +82,7 @@ func Import(ctx context.Context, db gorp.SqlExecutor, proj sdk.Project, app *sdk
for _, k := range app.Keys {
k.ApplicationID = app.ID
if err := InsertKey(db, &k); err != nil {
return sdk.WrapError(err, "Unable to insert key %s", k.Name)
return sdk.WrapError(err, "unable to insert key %s", k.Name)
}
if msgChan != nil {
msgChan <- sdk.NewMessage(sdk.MsgAppKeyCreated, strings.ToUpper(k.Type.String()), k.Name, app.Name)
Expand All @@ -92,13 +91,13 @@ func Import(ctx context.Context, db gorp.SqlExecutor, proj sdk.Project, app *sdk

//Set deployment strategies
if err := DeleteAllDeploymentStrategies(db, app.ID); err != nil {
return sdk.WrapError(err, "Unable to delete deployment strategies")
return sdk.WrapError(err, "unable to delete deployment strategies")
}

for pfName, pfConfig := range app.DeploymentStrategies {
pf, has := proj.GetIntegration(pfName)
if !has {
return sdk.WrapError(sdk.NewError(sdk.ErrNotFound, fmt.Errorf("integration %s not found", pfName)), "application.Import")
return sdk.NewErrorFrom(sdk.ErrNotFound, "integration %s not found", pfName)
}
if err := SetDeploymentStrategy(db, proj.ID, app.ID, pf.IntegrationModelID, pfName, pfConfig); err != nil {
return sdk.WrapError(err, "unable to set deployment strategy %s", pfName)
Expand Down
2 changes: 1 addition & 1 deletion engine/api/application/application_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ParseAndImport(ctx context.Context, db gorp.SqlExecutor, cache cache.Store,
}

if oldApp != nil && oldApp.FromRepository != "" && opts.FromRepository != oldApp.FromRepository {
return nil, msgList, sdk.WrapError(sdk.ErrApplicationAsCodeOverride, "unable to update as code application %s/%s.", oldApp.FromRepository, opts.FromRepository)
return nil, msgList, sdk.NewErrorFrom(sdk.ErrApplicationAsCodeOverride, "unable to update existing ascode application from %s", oldApp.FromRepository)
}

//Craft the application
Expand Down
8 changes: 3 additions & 5 deletions engine/api/application/dao_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package application

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -162,17 +161,16 @@ func InsertVariable(db gorp.SqlExecutor, appID int64, v *sdk.Variable, u sdk.Ide
//Check variable name
rx := sdk.NamePatternRegex
if !rx.MatchString(v.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid variable name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match pattern %s", sdk.NamePattern)
}

dbVar := newDBApplicationVariable(*v, appID)
err := gorpmapping.InsertAndSign(context.Background(), db, &dbVar)
if err != nil && strings.Contains(err.Error(), "application_variable_pkey") {
return sdk.WithStack(sdk.ErrVariableExists)

}
if err != nil {
return sdk.WrapError(err, "Cannot insert variable %s", v.Name)
return sdk.WrapError(err, "cannot insert variable %s", v.Name)
}

*v = dbVar.Variable()
Expand All @@ -196,7 +194,7 @@ func InsertVariable(db gorp.SqlExecutor, appID int64, v *sdk.Variable, u sdk.Ide
func UpdateVariable(db gorp.SqlExecutor, appID int64, variable *sdk.Variable, variableBefore *sdk.Variable, u sdk.Identifiable) error {
rx := sdk.NamePatternRegex
if !rx.MatchString(variable.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid variable name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match pattern %s", sdk.NamePattern)
}

dbVar := newDBApplicationVariable(*variable, appID)
Expand Down
13 changes: 6 additions & 7 deletions engine/api/environment/dao_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package environment

import (
"context"
"fmt"
"time"

"github.com/go-gorp/gorp"
Expand Down Expand Up @@ -108,16 +107,16 @@ func InsertVariable(db gorp.SqlExecutor, envID int64, v *sdk.Variable, u sdk.Ide
//Check variable name
rx := sdk.NamePatternRegex
if !rx.MatchString(v.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid variable name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match %s", sdk.NamePattern)
}

if sdk.NeedPlaceholder(v.Type) && v.Value == sdk.PasswordPlaceholder {
return fmt.Errorf("You try to insert a placeholder for new variable %s", v.Name)
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "you try to insert a placeholder for new variable %s", v.Name)
}

dbVar := newdbEnvironmentVariable(*v, envID)
if err := gorpmapping.InsertAndSign(context.Background(), db, &dbVar); err != nil {
return sdk.WrapError(err, "Cannot insert variable %s", v.Name)
return sdk.WrapError(err, "cannot insert variable %s", v.Name)
}

*v = dbVar.Variable()
Expand All @@ -132,7 +131,7 @@ func InsertVariable(db gorp.SqlExecutor, envID int64, v *sdk.Variable, u sdk.Ide
}

if err := insertAudit(db, ava); err != nil {
return sdk.WrapError(err, "Cannot insert audit for variable %d", v.ID)
return sdk.WrapError(err, "cannot insert audit for variable %d", v.ID)
}
return nil
}
Expand All @@ -141,7 +140,7 @@ func InsertVariable(db gorp.SqlExecutor, envID int64, v *sdk.Variable, u sdk.Ide
func UpdateVariable(db gorp.SqlExecutor, envID int64, variable *sdk.Variable, variableBefore *sdk.Variable, u sdk.Identifiable) error {
rx := sdk.NamePatternRegex
if !rx.MatchString(variable.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid variable name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "variable name should match %s", sdk.NamePattern)
}

dbVar := newdbEnvironmentVariable(*variable, envID)
Expand All @@ -167,7 +166,7 @@ func UpdateVariable(db gorp.SqlExecutor, envID int64, variable *sdk.Variable, va
}

if err := insertAudit(db, ava); err != nil {
return sdk.WrapError(err, "Cannot insert audit for variable %s", variable.Name)
return sdk.WrapError(err, "cannot insert audit for variable %s", variable.Name)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions engine/api/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ func CheckDefaultEnv(db gorp.SqlExecutor) error {
if err1 := db.QueryRow(query, sdk.DefaultEnv.Name).Scan(&env.ID); err1 != nil {
return err1
} else if env.ID != sdk.DefaultEnv.ID {
return fmt.Errorf("CheckDefaultEnv> default env created, but with wrong id. Please check db")
return sdk.WithStack(fmt.Errorf("default env created but with wrong id, please check db"))
}
return nil
}
return err
} else if env.ID != sdk.DefaultEnv.ID || env.Name != sdk.DefaultEnv.Name {
return fmt.Errorf("CheckDefaultEnv> default env exists, but with wrong id or name. Please check db")
return sdk.WithStack(fmt.Errorf("default env exists but with wrong id or name, please check db"))
}
return nil
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func InsertEnvironment(db gorp.SqlExecutor, env *sdk.Environment) error {

rx := sdk.NamePatternRegex
if !rx.MatchString(env.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid environment name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "environment name should match pattern %s", sdk.NamePattern)
}

err := db.QueryRow(query, env.Name, env.ProjectID, env.FromRepository).Scan(&env.ID, &env.Created, &env.LastModified)
Expand Down
2 changes: 1 addition & 1 deletion engine/api/environment/environment_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ExportEnvironment(db gorp.SqlExecutor, env sdk.Environment, encryptFunc sdk
for _, v := range env.Variables {
switch v.Type {
case sdk.KeyVariable:
return exportentities.Environment{}, sdk.WrapError(fmt.Errorf("Unsupported variable %s", v.Name), "environment.Export> Unable to export application")
return exportentities.Environment{}, sdk.NewErrorFrom(sdk.ErrWrongRequest, "unsupported variable %s", v.Name)
case sdk.SecretVariable:
content, err := encryptFunc(db, env.ProjectID, fmt.Sprintf("envID:%d:%s", env.ID, v.Name), v.Value)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion engine/api/environment/environment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ParseAndImport(db gorp.SqlExecutor, proj sdk.Project, eenv exportentities.E
}

if oldEnv != nil && oldEnv.FromRepository != "" && opts.FromRepository != oldEnv.FromRepository {
return nil, nil, sdk.WrapError(sdk.ErrEnvironmentAsCodeOverride, "unable to update as code environment %s/%s.", oldEnv.FromRepository, opts.FromRepository)
return nil, nil, sdk.NewErrorFrom(sdk.ErrEnvironmentAsCodeOverride, "unable to update existing ascode environment from %s", oldEnv.FromRepository)
}

env := new(sdk.Environment)
Expand Down
6 changes: 5 additions & 1 deletion engine/api/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ func (api *API) updateAsCodePipelineHandler() service.Handler {
branch := FormString(r, "branch")
message := FormString(r, "message")

if branch == "" || message == "" {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing branch or message data")
}

var p sdk.Pipeline
if err := service.UnmarshalBody(r, &p); err != nil {
return sdk.WrapError(err, "Cannot read body")
return err
}

// check pipeline name pattern
Expand Down
4 changes: 2 additions & 2 deletions engine/api/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func UpdatePipeline(db gorp.SqlExecutor, p *sdk.Pipeline) error {
p.LastModified = now.Unix()
rx := sdk.NamePatternRegex
if !rx.MatchString(p.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid pipeline name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "pipeline name should match %s", sdk.NamePattern)
}

//Update pipeline
Expand All @@ -350,7 +350,7 @@ func InsertPipeline(db gorp.SqlExecutor, p *sdk.Pipeline) error {

rx := sdk.NamePatternRegex
if !rx.MatchString(p.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("invalid pipeline name, should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "pipeline name should match %s", sdk.NamePattern)
}

if p.ProjectID == 0 {
Expand Down
3 changes: 1 addition & 2 deletions engine/api/pipeline/pipeline_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pipeline

import (
"context"
"fmt"
"time"

"github.com/go-gorp/gorp"
Expand Down Expand Up @@ -58,7 +57,7 @@ func ImportUpdate(ctx context.Context, db gorp.SqlExecutor, proj sdk.Project, pi
s := &pip.Stages[i]
// stage name mandatory if there are many stages
if len(pip.Stages) > 1 && !rx.MatchString(s.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid stage name '%s'. It should match %s", s.Name, sdk.NamePatternSpace))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "stage name '%s' should match pattern %s", s.Name, sdk.NamePatternSpace)
}

//Insert stage
Expand Down
20 changes: 9 additions & 11 deletions engine/api/pipeline/pipeline_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pipeline

import (
"context"
"fmt"

"github.com/go-gorp/gorp"
"github.com/lib/pq"
Expand Down Expand Up @@ -58,12 +57,12 @@ func GetAllParametersInPipeline(ctx context.Context, db gorp.SqlExecutor, pipeli
// InsertParameterInPipeline Insert a new parameter in the given pipeline
func InsertParameterInPipeline(db gorp.SqlExecutor, pipelineID int64, param *sdk.Parameter) error {
if param.Type == "" {
return sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("Invalid parameter, wrong type"))
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid parameter, wrong type")
}

rx := sdk.NamePatternRegex
if !rx.MatchString(param.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid parameter name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "parameter name should match %s", sdk.NamePattern)
}

if string(param.Type) == string(sdk.SecretVariable) {
Expand All @@ -83,22 +82,21 @@ func InsertParameterInPipeline(db gorp.SqlExecutor, pipelineID int64, param *sdk
// UpdateParameterInPipeline Update a parameter in the given pipeline
func UpdateParameterInPipeline(db gorp.SqlExecutor, pipelineID int64, oldParamName string, param sdk.Parameter) error {
if param.Type == "" {
return sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("Invalid parameter, wrong type"))
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid parameter, wrong type")
}

rx := sdk.NamePatternRegex
if !rx.MatchString(param.Name) {
return sdk.NewError(sdk.ErrInvalidName, fmt.Errorf("Invalid parameter name. It should match %s", sdk.NamePattern))
return sdk.NewErrorFrom(sdk.ErrInvalidName, "parameter name should match pattern %s", sdk.NamePattern)
}

// update parameter
query := `UPDATE pipeline_parameter SET value=$1, type=$2, description=$3, name=$4 WHERE pipeline_id=$5 AND name=$6`
_, err := db.Exec(query, param.Value, string(param.Type), param.Description, param.Name, pipelineID, oldParamName)
if err != nil {
if _, err := db.Exec(query, param.Value, string(param.Type), param.Description, param.Name, pipelineID, oldParamName); err != nil {
if errPG, ok := err.(*pq.Error); ok && errPG.Code == gorpmapping.ViolateUniqueKeyPGCode {
return sdk.WithStack(sdk.ErrParameterExists)
return sdk.NewErrorWithStack(err, sdk.ErrParameterExists)
}
return err
return sdk.WithStack(err)
}

return nil
Expand All @@ -109,7 +107,7 @@ func DeleteParameterFromPipeline(db gorp.SqlExecutor, pipelineID int64, paramNam
query := `DELETE FROM pipeline_parameter WHERE pipeline_id=$1 AND name=$2`
_, err := db.Exec(query, pipelineID, paramName)
if err != nil {
return err
return sdk.WithStack(err)
}

return nil
Expand All @@ -119,5 +117,5 @@ func DeleteParameterFromPipeline(db gorp.SqlExecutor, pipelineID int64, paramNam
func DeleteAllParameterFromPipeline(db gorp.SqlExecutor, pipelineID int64) error {
query := `DELETE FROM pipeline_parameter WHERE pipeline_id=$1`
_, err := db.Exec(query, pipelineID)
return sdk.WrapError(err, "Unable to delete all parameters")
return sdk.WrapError(err, "unable to delete all parameters")
}
4 changes: 4 additions & 0 deletions engine/api/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func TestUpdateAsCodePipelineHandler(t *testing.T) {
"pipelineKey": pip.Name,
})
req := assets.NewJWTAuthentifiedRequest(t, pass, "PUT", uri, pip)
q := req.URL.Query()
q.Set("branch", "master")
q.Set("message", "my message")
req.URL.RawQuery = q.Encode()

// Do the request
wr := httptest.NewRecorder()
Expand Down
17 changes: 7 additions & 10 deletions engine/api/repositoriesmanager/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,33 @@ func RetryEvent(e *sdk.Event, err error, store cache.Store) error {
}

func processEvent(ctx context.Context, db *gorp.DbMap, event sdk.Event, store cache.Store) error {
var c sdk.VCSAuthorizedClientService
var errC error

if event.EventType != fmt.Sprintf("%T", sdk.EventRunWorkflowNode{}) {
return nil
}

var eventWNR sdk.EventRunWorkflowNode

if err := json.Unmarshal(event.Payload, &eventWNR); err != nil {
return fmt.Errorf("cannot read payload: %v", err)
return sdk.WrapError(err, "cannot read payload")
}
if eventWNR.RepositoryManagerName == "" {
return nil
}
vcsServer, err := LoadProjectVCSServerLinkByProjectKeyAndVCSServerName(ctx, db, event.ProjectKey, eventWNR.RepositoryManagerName)
if err != nil {
return fmt.Errorf("repositoriesmanager>processEvent> AuthorizedClient (%s, %s) > err:%s", event.ProjectKey, eventWNR.RepositoryManagerName, err)
return sdk.WrapError(err, "AuthorizedClient (%s, %s)", event.ProjectKey, eventWNR.RepositoryManagerName)
}

c, err = AuthorizedClient(ctx, db, store, event.ProjectKey, vcsServer)
c, err := AuthorizedClient(ctx, db, store, event.ProjectKey, vcsServer)
if err != nil {
return fmt.Errorf("repositoriesmanager>processEvent> AuthorizedClient (%s, %s) > err:%s", event.ProjectKey, eventWNR.RepositoryManagerName, errC)
return sdk.WrapError(err, "AuthorizedClient (%s, %s)", event.ProjectKey, eventWNR.RepositoryManagerName)
}

if err := c.SetStatus(ctx, event); err != nil {
if err2 := RetryEvent(&event, err, store); err2 != nil {
log.Error(ctx, "repositoriesmanager>processEvent> err while retry event: %v", err2)
if err := RetryEvent(&event, err, store); err != nil {
log.Error(ctx, "repositoriesmanager>processEvent> err while retry event: %v", err)
}
return fmt.Errorf("repositoriesmanager>processEvent> SetStatus > event.EventType:%s err:%s", event.EventType, err)
return sdk.WrapError(err, "event.EventType: %s", event.EventType)
}

return nil
Expand Down
Loading

0 comments on commit 02c2eab

Please sign in to comment.