Skip to content

Commit 970c539

Browse files
authoredJul 3, 2024
wrap ErrPanicRecovered with recoverData (#749)
1 parent 9d27ea8 commit 970c539

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
 

‎executor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gocron
22

33
import (
44
"context"
5+
"fmt"
56
"strconv"
67
"sync"
78
"time"
@@ -387,7 +388,7 @@ func (e *executor) callJobWithRecover(j internalJob) (err error) {
387388
_ = callJobFuncWithParams(j.afterJobRunsWithPanic, j.id, j.name, recoverData)
388389

389390
// if panic is occurred, we should return an error
390-
err = ErrPanicRecovered
391+
err = fmt.Errorf("%w from %v", ErrPanicRecovered, recoverData)
391392
}
392393
}()
393394

‎job_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ func TestJob_NextRuns(t *testing.T) {
646646

647647
func TestJob_PanicOccurred(t *testing.T) {
648648
gotCh := make(chan any)
649+
errCh := make(chan error)
649650
s := newTestScheduler(t)
650651
_, err := s.NewJob(
651652
DurationJob(10*time.Millisecond),
@@ -656,6 +657,8 @@ func TestJob_PanicOccurred(t *testing.T) {
656657
WithEventListeners(
657658
AfterJobRunsWithPanic(func(_ uuid.UUID, _ string, recoverData any) {
658659
gotCh <- recoverData
660+
}), AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
661+
errCh <- err
659662
}),
660663
),
661664
)
@@ -665,6 +668,11 @@ func TestJob_PanicOccurred(t *testing.T) {
665668
got := <-gotCh
666669
require.EqualError(t, got.(error), "runtime error: integer divide by zero")
667670

671+
err = <-errCh
672+
require.ErrorIs(t, err, ErrPanicRecovered)
673+
require.EqualError(t, err, "gocron: panic recovered from runtime error: integer divide by zero")
674+
668675
require.NoError(t, s.Shutdown())
669676
close(gotCh)
677+
close(errCh)
670678
}

0 commit comments

Comments
 (0)