Skip to content

Commit

Permalink
ctx: Use Cause to return different errors
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhicks committed Oct 10, 2024
1 parent 3d0dd22 commit 816b9df
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
16 changes: 8 additions & 8 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (a *App) startup(ctx context.Context) error {
defer pprof.SetGoroutineLabels(ctx)

for idx, h := range a.startupHooks {
if ctx.Err() != nil {
return ctx.Err()
if context.Cause(ctx) != nil {
return context.Cause(ctx)
}
a.OnEvent(ctx, Event{Type: PreHookStart, Name: h.Name})
hookCtx := ctx
Expand All @@ -114,14 +114,14 @@ func (a *App) startup(ctx context.Context) error {
}
a.OnEvent(ctx, Event{Type: PostHookStart, Name: h.Name})
}
return ctx.Err()
return context.Cause(ctx)
}

func (a *App) runShutdownHooks(ctx context.Context) error {
var errs []error
for idx, h := range a.shutdownHooks {
if ctx.Err() != nil {
return ctx.Err()
if context.Cause(ctx) != nil {
return context.Cause(ctx)
}
a.OnEvent(ctx, Event{Type: PreHookStop, Name: h.Name})
hookCtx := log.ContextWith(ctx, j.MKV{"hook_idx": idx, "hook_name": h.Name})
Expand Down Expand Up @@ -233,7 +233,7 @@ func (a *App) Launch(ctx context.Context) error {
})
}
a.OnEvent(ctx, Event{Type: AppRunning})
return ctx.Err()
return context.Cause(ctx)
}

// WaitForShutdown returns a channel that waits for the application to be cancelled.
Expand Down Expand Up @@ -331,7 +331,7 @@ func (a *App) cleanup(ctx context.Context) {
// It will return an error if cancelled early.
func Wait(ctx context.Context, cl clock.Clock, d time.Duration) error {
if d <= 0 {
return ctx.Err()
return context.Cause(ctx)
}
ti := cl.NewTimer(d)
defer ti.Stop()
Expand All @@ -357,7 +357,7 @@ func WaitFor[T any](ctx context.Context, ch <-chan T) (T, error) {
return v, nil
case <-ctx.Done():
var v T
return v, ctx.Err()
return v, context.Cause(ctx)
}
}

Expand Down
10 changes: 5 additions & 5 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ func TestLifecycle(t *testing.T) {
Run: func(ctx context.Context) error {
log.Info(ctx, "one")
<-ctx.Done()
return ctx.Err()
return context.Cause(ctx)
},
},
lu.Process{
Name: "two",
Run: func(ctx context.Context) error {
log.Info(ctx, "two")
<-ctx.Done()
return ctx.Err()
return context.Cause(ctx)
},
},
lu.Process{
Name: "three",
Run: func(ctx context.Context) error {
log.Info(ctx, "three")
<-ctx.Done()
return ctx.Err()
return context.Cause(ctx)
},
},
process.ContextLoop(
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestShutdownWithParentContext(t *testing.T) {
a.AddProcess(lu.Process{
Run: func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
return context.Cause(ctx)
},
})

Expand Down Expand Up @@ -140,7 +140,7 @@ func TestProcessShutdown(t *testing.T) {
a.ShutdownTimeout = 100 * time.Millisecond
a.AddProcess(lu.Process{Shutdown: func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
return context.Cause(ctx)
}})
},
expErr: context.DeadlineExceeded,
Expand Down
4 changes: 2 additions & 2 deletions process/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func wrapContextLoop(getCtx ContextFunc, f lu.ProcessFunc, opts options) lu.Proc
return err
}
}
return ctx.Err()
return context.Cause(ctx)
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func ContextRetry(
break
}
}
return ctx.Err()
return context.Cause(ctx)
}
return p
}
Expand Down
2 changes: 1 addition & 1 deletion process/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func NoOp() lu.Process {
Name: "noop",
Run: func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
return context.Cause(ctx)
},
}
}
4 changes: 2 additions & 2 deletions process/reflex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *consumer) Stop() error {
func Test_ReflexConsumer_afterLoop(t *testing.T) {
awaitFunc := func(role string) func(ctx context.Context) (context.Context, context.CancelFunc, error) {
return func(ctx context.Context) (context.Context, context.CancelFunc, error) {
return ctx, func() {}, ctx.Err()
return ctx, func() {}, context.Cause(ctx)
}
}
makeStream := func(ctx context.Context, after string, opts ...reflex.StreamOption) (reflex.StreamClient, error) {
Expand All @@ -63,7 +63,7 @@ func Test_ReflexConsumer_afterLoop(t *testing.T) {
func Test_ReflexConsumer_breakLoop(t *testing.T) {
awaitFunc := func(role string) func(ctx context.Context) (context.Context, context.CancelFunc, error) {
return func(ctx context.Context) (context.Context, context.CancelFunc, error) {
return ctx, func() {}, ctx.Err()
return ctx, func() {}, context.Cause(ctx)
}
}
makeStream := func(ctx context.Context, after string, opts ...reflex.StreamOption) (reflex.StreamClient, error) {
Expand Down
2 changes: 1 addition & 1 deletion process/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func processLoop(ctx context.Context, process processFunc, wait waitFunc) error
return err
}
}
return ctx.Err()
return context.Cause(ctx)
}

// processOnce may panic if awaitRole is nil or if when calling it returns a nil role.ContextFunc, and
Expand Down

0 comments on commit 816b9df

Please sign in to comment.