Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings-zeebe-command): Add support for passing variables to "throw-error" operation #3640

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions bindings/zeebe/command/throw_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ import (
"errors"
"fmt"

"github.com/camunda/zeebe/clients/go/v8/pkg/commands"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is imported but unused. You'll have to remove it or the linter will not pass this.

Run make modtidy-all for the linter check.


"github.com/dapr/components-contrib/bindings"
)

var ErrMissingErrorCode = errors.New("errorCode is a required attribute")

type throwErrorPayload struct {
JobKey *int64 `json:"jobKey"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
JobKey *int64 `json:"jobKey"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
Variables interface{} `json:"variables"`
}

func (z *ZeebeCommand) throwError(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
Expand All @@ -53,7 +56,15 @@ func (z *ZeebeCommand) throwError(ctx context.Context, req *bindings.InvokeReque
cmd = cmd.ErrorMessage(payload.ErrorMessage)
}

_, err = cmd.Send(ctx)
var cmdDispatch = cmd
if payload.Variables != nil {
cmdDispatch, err = cmd.VariablesFromObject(payload.Variables)
if err != nil {
return nil, err
}
}

_, err = cmdDispatch.Send(ctx)
if err != nil {
return nil, fmt.Errorf("cannot throw error for job key %d: %w", payload.JobKey, err)
}
Expand Down
33 changes: 33 additions & 0 deletions bindings/zeebe/command/throw_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type mockThrowErrorCommandStep2 struct {
type mockDispatchThrowErrorCommand struct {
commands.DispatchThrowErrorCommand
errorMessage string
variables interface{}
}

func (mc *mockThrowErrorClient) NewThrowErrorCommand() commands.ThrowErrorCommandStep1 {
Expand Down Expand Up @@ -78,6 +79,12 @@ func (cmd3 *mockDispatchThrowErrorCommand) ErrorMessage(errorMessage string) com
return cmd3
}

func (cmd3 *mockDispatchThrowErrorCommand) VariablesFromObject(variables interface{}) (commands.DispatchThrowErrorCommand, error) {
cmd3.variables = variables

return cmd3, nil
}

func (cmd3 *mockDispatchThrowErrorCommand) Send(context.Context) (*pb.ThrowErrorResponse, error) {
return &pb.ThrowErrorResponse{}, nil
}
Expand Down Expand Up @@ -129,4 +136,30 @@ func TestThrowError(t *testing.T) {
assert.Equal(t, payload.ErrorCode, mc.cmd1.cmd2.errorCode)
assert.Equal(t, payload.ErrorMessage, mc.cmd1.cmd2.cmd3.errorMessage)
})

t.Run("throw an error with variables", func(t *testing.T) {
payload := throwErrorPayload{
JobKey: new(int64),
ErrorCode: "a",
ErrorMessage: "b",
Variables: map[string]interface{}{
"key": "value",
},
}
data, err := json.Marshal(payload)
require.NoError(t, err)

req := &bindings.InvokeRequest{Data: data, Operation: ThrowErrorOperation}

var mc mockThrowErrorClient

cmd := ZeebeCommand{logger: testLogger, client: &mc}
_, err = cmd.Invoke(context.TODO(), req)
require.NoError(t, err)

assert.Equal(t, *payload.JobKey, mc.cmd1.jobKey)
assert.Equal(t, payload.ErrorCode, mc.cmd1.cmd2.errorCode)
assert.Equal(t, payload.ErrorMessage, mc.cmd1.cmd2.cmd3.errorMessage)
assert.Equal(t, payload.Variables, mc.cmd1.cmd2.cmd3.variables)
})
}
Loading