diff --git a/pkg/crud/types.go b/pkg/crud/types.go index 0853944..bcf72dd 100644 --- a/pkg/crud/types.go +++ b/pkg/crud/types.go @@ -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 } @@ -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) +} diff --git a/pkg/crud/types_test.go b/pkg/crud/types_test.go index 987cc39..b8ffe9b 100644 --- a/pkg/crud/types_test.go +++ b/pkg/crud/types_test.go @@ -1,6 +1,7 @@ package crud import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -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()) +} diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go index e4a8eb3..7820642 100644 --- a/pkg/diff/diff.go +++ b/pkg/diff/diff.go @@ -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