-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a539f59
commit f890c83
Showing
5 changed files
with
66 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package packet | ||
|
||
import "github.com/siyul-park/uniflow/pkg/primitive" | ||
|
||
// NewError creates a new Packet to represent an error. | ||
// It takes an error and an optional cause Packet and constructs a Packet with error details. | ||
func NewError(err error, cause *Packet) *Packet { | ||
pairs := []primitive.Value{ | ||
primitive.NewString("error"), | ||
primitive.NewString(err.Error()), | ||
} | ||
|
||
if cause != nil { | ||
pairs = append(pairs, primitive.NewString("cause"), cause.Payload()) | ||
} | ||
|
||
return New(primitive.NewMap(pairs...)) | ||
} | ||
|
||
// IsError checks if the given Packet represents an error. | ||
func IsError(pck *Packet) bool { | ||
payload := pck.Payload() | ||
_, ok := primitive.Pick[string](payload, "error") | ||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package packet | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/go-faker/faker/v4" | ||
"github.com/siyul-park/uniflow/pkg/primitive" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewError(t *testing.T) { | ||
err := errors.New(faker.Sentence()) | ||
|
||
pck1 := New(primitive.NewString(faker.Word())) | ||
pck2 := NewError(err, pck1) | ||
|
||
assert.NotNil(t, pck2) | ||
assert.NotZero(t, pck2.ID()) | ||
|
||
payload, ok := pck2.Payload().(*primitive.Map) | ||
assert.True(t, ok) | ||
assert.Equal(t, err.Error(), payload.GetOr(primitive.NewString("error"), nil).Interface()) | ||
assert.Equal(t, pck1.Payload(), payload.GetOr(primitive.NewString("cause"), nil)) | ||
} | ||
|
||
func TestIsError(t *testing.T) { | ||
err := errors.New(faker.Sentence()) | ||
|
||
pck1 := New(primitive.NewString(faker.Word())) | ||
pck2 := NewError(err, pck1) | ||
|
||
assert.True(t, IsError(pck2)) | ||
assert.False(t, IsError(pck1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters