Skip to content

Commit

Permalink
Add TerminalErrorf (#30)
Browse files Browse the repository at this point in the history
* Add TerminalErrorf

* Use whenever possible TerminalErrorf
  • Loading branch information
slinkydeveloper authored Aug 21, 2024
1 parent 333963c commit 16fb256
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 6 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package restate

import (
stderrors "errors"
"fmt"

"github.com/restatedev/sdk-go/internal/errors"
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test-services/counter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"fmt"

restate "github.com/restatedev/sdk-go"
)

Expand Down Expand Up @@ -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())
})))
}
6 changes: 3 additions & 3 deletions test-services/failing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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) {
if err := ctx.Object("Failing", ctx.Rand().UUID().String(), "terminallyFailingCall").Request(errorMessage, restate.Void{}); err != nil {
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) {
Expand All @@ -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)
})
})))
}
3 changes: 1 addition & 2 deletions test-services/testutils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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
})).
Expand Down

0 comments on commit 16fb256

Please sign in to comment.