Skip to content

Commit

Permalink
feat: improve error handel
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 2, 2023
1 parent a539f59 commit f890c83
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 34 deletions.
25 changes: 25 additions & 0 deletions pkg/packet/error.go
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
}
35 changes: 35 additions & 0 deletions pkg/packet/error_test.go
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))
}
15 changes: 0 additions & 15 deletions pkg/packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ type Packet struct {
payload primitive.Value
}

// 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...))
}

// New creates a new Packet with the given payload.
// It generates a new unique ID for the Packet.
func New(payload primitive.Value) *Packet {
Expand Down
18 changes: 0 additions & 18 deletions pkg/packet/packet_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
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 TestNew(t *testing.T) {
pck := New(nil)
assert.NotNil(t, pck)
Expand Down
7 changes: 6 additions & 1 deletion pkg/plugin/networkx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {
proc.Stack().Push(outPck.ID(), outStream.ID())
outStream.Send(outPck)
}

if ioStream.Links()+outStream.Links() == 0 {
return
}
Expand Down Expand Up @@ -450,7 +451,11 @@ func (n *HTTPNode) ServeHTTP(w http.ResponseWriter, r *http.Request) {

var res HTTPPayload
if err := primitive.Unmarshal(inPayload, &res); err != nil {
res.Body = inPayload
if packet.IsError(inPck) {
res = InternalServerError
} else {
res.Body = inPayload
}
}

if err := n.response(r, w, res); err != nil {
Expand Down

0 comments on commit f890c83

Please sign in to comment.