Skip to content

Commit

Permalink
wrap CRUD action error
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Jan 18, 2024
1 parent f1e0361 commit 6e35645
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
19 changes: 17 additions & 2 deletions pkg/crud/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package crud

import "context"
import (
"context"
"fmt"
)

// Op represents
// Op represents the type of the operation.
type Op struct {
name string
}
Expand Down Expand Up @@ -49,3 +52,15 @@ func EventFromArg(arg Arg) Event {
}
return event
}

// ActionError represents an error happens in performing CRUD action of an entity.
type ActionError struct {
OperationType Op `json:"operation"`
Kind Kind `json:"kind"`
Name string `json:"name"`
Err error `json:"error"`
}

func (e *ActionError) Error() string {
return fmt.Sprintf("%s %s %s failed: %v", e.OperationType.String(), e.Kind, e.Name, e.Err)
}
12 changes: 12 additions & 0 deletions pkg/crud/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crud

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,3 +14,14 @@ func TestOpString(t *testing.T) {
assert.Equal("foo", op.String())
assert.Equal("", op2.String())
}

func TestActionError(t *testing.T) {
err := fmt.Errorf("something wrong")
actionErr := &ActionError{
OperationType: Create,
Kind: Kind("service"),
Name: "service-test",
Err: err,
}
assert.Equal(t, "Create service service-test failed: something wrong", actionErr.Error())
}
7 changes: 6 additions & 1 deletion pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,12 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
// fire the request to Kong
result, err = sc.processor.Do(ctx, e.Kind, e.Op, e)
if err != nil {
return nil, fmt.Errorf("%v %v %v failed: %w", e.Op, e.Kind, c.Console(), err)
return nil, &crud.ActionError{
OperationType: e.Op,
Kind: e.Kind,
Name: c.Console(),
Err: err,
}
}
} else {
// diff mode
Expand Down

0 comments on commit 6e35645

Please sign in to comment.