mirror of https://github.com/go-co-op/gocron.git
wrap ErrPanicRecovered with recoverData (#749)
This commit is contained in:
parent
9d27ea8673
commit
970c5399bc
|
|
@ -2,6 +2,7 @@ package gocron
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -387,7 +388,7 @@ func (e *executor) callJobWithRecover(j internalJob) (err error) {
|
||||||
_ = callJobFuncWithParams(j.afterJobRunsWithPanic, j.id, j.name, recoverData)
|
_ = callJobFuncWithParams(j.afterJobRunsWithPanic, j.id, j.name, recoverData)
|
||||||
|
|
||||||
// if panic is occurred, we should return an error
|
// if panic is occurred, we should return an error
|
||||||
err = ErrPanicRecovered
|
err = fmt.Errorf("%w from %v", ErrPanicRecovered, recoverData)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -646,6 +646,7 @@ func TestJob_NextRuns(t *testing.T) {
|
||||||
|
|
||||||
func TestJob_PanicOccurred(t *testing.T) {
|
func TestJob_PanicOccurred(t *testing.T) {
|
||||||
gotCh := make(chan any)
|
gotCh := make(chan any)
|
||||||
|
errCh := make(chan error)
|
||||||
s := newTestScheduler(t)
|
s := newTestScheduler(t)
|
||||||
_, err := s.NewJob(
|
_, err := s.NewJob(
|
||||||
DurationJob(10*time.Millisecond),
|
DurationJob(10*time.Millisecond),
|
||||||
|
|
@ -656,6 +657,8 @@ func TestJob_PanicOccurred(t *testing.T) {
|
||||||
WithEventListeners(
|
WithEventListeners(
|
||||||
AfterJobRunsWithPanic(func(_ uuid.UUID, _ string, recoverData any) {
|
AfterJobRunsWithPanic(func(_ uuid.UUID, _ string, recoverData any) {
|
||||||
gotCh <- recoverData
|
gotCh <- recoverData
|
||||||
|
}), AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
|
||||||
|
errCh <- err
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
@ -665,6 +668,11 @@ func TestJob_PanicOccurred(t *testing.T) {
|
||||||
got := <-gotCh
|
got := <-gotCh
|
||||||
require.EqualError(t, got.(error), "runtime error: integer divide by zero")
|
require.EqualError(t, got.(error), "runtime error: integer divide by zero")
|
||||||
|
|
||||||
|
err = <-errCh
|
||||||
|
require.ErrorIs(t, err, ErrPanicRecovered)
|
||||||
|
require.EqualError(t, err, "gocron: panic recovered from runtime error: integer divide by zero")
|
||||||
|
|
||||||
require.NoError(t, s.Shutdown())
|
require.NoError(t, s.Shutdown())
|
||||||
close(gotCh)
|
close(gotCh)
|
||||||
|
close(errCh)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue