Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

feat(pool options): add ability to use custom error encoder #6

Merged
merged 3 commits into from
Feb 9, 2022
Merged
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
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# CHANGELOG

# v2.8.0-rc.4 (09.02.2022)

## 👀 New:

- ✏️ Pool: add a new option to override the custom error encoder: `pool.WithCustomErrEncoder`.

---

# v2.8.0-rc.3 (04.02.2022)

## 📦 Packages:

- 📦 tcplisten `v1.1.1`

---

# v2.8.0-rc.2 (29.01.2022)

## 📦 Packages:

- 📦 goridge `v3.3.1`

---

# v2.8.0-rc.1 (21.01.2022)

## 👀 New:
Expand Down
17 changes: 17 additions & 0 deletions pool/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pool

import (
"go.uber.org/zap"
)

func WithLogger(z *zap.Logger) Options {
return func(p *StaticPool) {
p.log = z
}
}

func WithCustomErrEncoder(errEnc ErrorEncoder) Options {
return func(p *StaticPool) {
p.errEncoder = errEnc
}
}
10 changes: 2 additions & 8 deletions pool/static_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func NewStaticPool(ctx context.Context, cmd Command, factory ipc.Factory, cfg *C
factory: factory,
}

p.errEncoder = defaultErrEncoder(p)

// add pool options
for i := 0; i < len(options); i++ {
options[i](p)
Expand Down Expand Up @@ -98,8 +100,6 @@ func NewStaticPool(ctx context.Context, cmd Command, factory ipc.Factory, cfg *C
return nil, err
}

p.errEncoder = defaultErrEncoder(p)

// if supervised config not nil, guess, that pool wanted to be supervised
if cfg.Supervisor != nil {
sp := supervisorWrapper(p, p.log, p.cfg.Supervisor)
Expand All @@ -111,12 +111,6 @@ func NewStaticPool(ctx context.Context, cmd Command, factory ipc.Factory, cfg *C
return p, nil
}

func WithLogger(z *zap.Logger) Options {
return func(p *StaticPool) {
p.log = z
}
}

// GetConfig returns associated pool configuration. Immutable.
func (sp *StaticPool) GetConfig() interface{} {
return sp.cfg
Expand Down
24 changes: 24 additions & 0 deletions pool/static_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ func Test_StaticPool_Echo_Context(t *testing.T) {
assert.Equal(t, "world", string(res.Context))
}

func Test_StaticPool_Echo_CustomErrEncoder(t *testing.T) {
ctx := context.Background()
p, err := NewStaticPool(
ctx,
func() *exec.Cmd { return exec.Command("php", "../tests/client.php", "error", "pipes") },
pipe.NewPipeFactory(log),
cfg,
WithCustomErrEncoder(func(err error, w worker.BaseProcess) (*payload.Payload, error) {
return nil, errors.E(errors.Str("foo"))
}),
)
assert.NoError(t, err)

defer p.Destroy(ctx)

assert.NotNil(t, p)

res, err := p.Exec(&payload.Payload{Body: []byte("hello"), Context: []byte("world")})

assert.Error(t, err)
assert.Nil(t, res)
assert.Contains(t, err.Error(), "foo")
}

func Test_StaticPool_JobError(t *testing.T) {
ctx := context.Background()
p, err := NewStaticPool(
Expand Down