diff --git a/error.go b/error.go index 4e3f60c..25c2599 100644 --- a/error.go +++ b/error.go @@ -2,6 +2,7 @@ package restate import ( stderrors "errors" + "fmt" "github.com/restatedev/sdk-go/internal/errors" ) @@ -32,6 +33,11 @@ func TerminalError(err error, code ...errors.Code) error { return errors.NewTerminalError(err, code...) } +// TerminalErrorf is a shorthand for combining fmt.Errorf with TerminalError +func TerminalErrorf(format string, a ...any) error { + return TerminalError(fmt.Errorf(format, a...)) +} + // IsTerminalError checks if err is terminal - ie, that returning it in a handler or Run function will finish // the invocation with the error as a result. func IsTerminalError(err error) bool { diff --git a/error_test.go b/error_test.go index 36e471d..ae9b612 100644 --- a/error_test.go +++ b/error_test.go @@ -11,7 +11,7 @@ import ( func TestTerminal(t *testing.T) { require.False(t, IsTerminalError(fmt.Errorf("not terminal"))) - err := TerminalError(fmt.Errorf("failed terminally")) + err := TerminalErrorf("failed terminally") require.True(t, IsTerminalError(err)) //terminal with code diff --git a/test-services/counter.go b/test-services/counter.go index f89405d..f064e2b 100644 --- a/test-services/counter.go +++ b/test-services/counter.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - restate "github.com/restatedev/sdk-go" ) @@ -50,6 +48,6 @@ func init() { newValue := oldValue + addend ctx.Set(COUNTER_KEY, newValue) - return restate.Void{}, restate.TerminalError(fmt.Errorf("%s", ctx.Key())) + return restate.Void{}, restate.TerminalErrorf("%s", ctx.Key()) }))) } diff --git a/test-services/failing.go b/test-services/failing.go index b5db52c..43d829d 100644 --- a/test-services/failing.go +++ b/test-services/failing.go @@ -15,7 +15,7 @@ func init() { restate.NewObject("Failing"). Handler("terminallyFailingCall", restate.NewObjectHandler( func(ctx restate.ObjectContext, errorMessage string) (restate.Void, error) { - return restate.Void{}, restate.TerminalError(fmt.Errorf(errorMessage)) + return restate.Void{}, restate.TerminalErrorf(errorMessage) })). Handler("callTerminallyFailingCall", restate.NewObjectHandler( func(ctx restate.ObjectContext, errorMessage string) (string, error) { @@ -23,7 +23,7 @@ func init() { return "", err } - return "", restate.TerminalError(fmt.Errorf("This should be unreachable")) + return "", restate.TerminalErrorf("This should be unreachable") })). Handler("failingCallWithEventualSuccess", restate.NewObjectHandler( func(ctx restate.ObjectContext, _ restate.Void) (int32, error) { @@ -50,7 +50,7 @@ func init() { Handler("terminallyFailingSideEffect", restate.NewObjectHandler( func(ctx restate.ObjectContext, errorMessage string) (restate.Void, error) { return restate.RunAs(ctx, func(ctx restate.RunContext) (restate.Void, error) { - return restate.Void{}, restate.TerminalError(fmt.Errorf(errorMessage)) + return restate.Void{}, restate.TerminalErrorf(errorMessage) }) }))) } diff --git a/test-services/testutils.go b/test-services/testutils.go index 7c72a6c..7156706 100644 --- a/test-services/testutils.go +++ b/test-services/testutils.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "strings" "sync/atomic" "time" @@ -83,7 +82,7 @@ func init() { i++ } if i != len(timers) { - return restate.Void{}, restate.TerminalError(fmt.Errorf("unexpected number of timers fired: %d", i)) + return restate.Void{}, restate.TerminalErrorf("unexpected number of timers fired: %d", i) } return restate.Void{}, nil })).