Skip to content

Commit 9c58e5a

Browse files
committed
Improves wording, test and conditional check
1 parent 11ad4d2 commit 9c58e5a

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

pipe/pipeline.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ func WithEventHandler(handler func(e *Event)) Option {
181181
}
182182

183183
// WithStagePanicHandler sets a panic handler for the stages within a pipeline.
184-
// When a stage within the pipeline panics, the provided handler will be invoked, enabling
185-
// clients to capture the panic, such as for observability purposes.
184+
// When a pipeline stage panics, the provided handler will be invoked, allowing
185+
// the client to handle the panic in whatever way they see fit.
186186
//
187187
// Note:
188-
// - The client is responsible for deciding whether to recover from the panic or propagate it further.
189-
// - Currently, only the Function stage supports this functionality.
188+
// - Only the Function stage supports this functionality.
189+
// - The client is responsible for deciding whether to recover from the panic or panicking again.
190+
// - If a panic handler is not set, the panic will be propagated normally.
190191
func WithStagePanicHandler(ph StagePanicHandler) Option {
191192
return func(p *Pipeline) {
192193
p.panicHandler = ph
@@ -279,10 +280,8 @@ func (p *Pipeline) Start(ctx context.Context) error {
279280
}
280281

281282
for i, s := range p.stages {
282-
if p.panicHandler != nil {
283-
if phs, ok := s.(StagePanicHandlerAware); ok {
284-
phs.SetPanicHandler(p.panicHandler)
285-
}
283+
if phs, ok := s.(StagePanicHandlerAware); ok && p.panicHandler != nil {
284+
phs.SetPanicHandler(p.panicHandler)
286285
}
287286

288287
var err error

pipe/pipeline_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,11 @@ func TestFunction(t *testing.T) {
462462
})
463463

464464
t.Run("panic with handler", func(t *testing.T) {
465-
panicked := make(chan bool, 1)
465+
panickedMessage := make(chan error, 1)
466466
p := pipe.New(
467467
pipe.WithDir(dir),
468468
pipe.WithStagePanicHandler(func(err error) {
469-
panicked <- true
470-
assert.Equal(t, "this is a panic", err.Error())
469+
panickedMessage <- err
471470
}),
472471
)
473472
p.Add(
@@ -481,7 +480,7 @@ func TestFunction(t *testing.T) {
481480
)
482481

483482
out, err := p.Output(ctx)
484-
assert.True(t, <-panicked)
483+
assert.Error(t, <-panickedMessage)
485484
assert.Error(t, err)
486485
assert.Empty(t, out)
487486
})

0 commit comments

Comments
 (0)