wrap ErrPanicRecovered with recoverData (#749)

This commit is contained in:
Higan 2024-07-03 23:23:45 +08:00 committed by GitHub
parent 9d27ea8673
commit 970c5399bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package gocron
import (
"context"
"fmt"
"strconv"
"sync"
"time"
@ -387,7 +388,7 @@ func (e *executor) callJobWithRecover(j internalJob) (err error) {
_ = callJobFuncWithParams(j.afterJobRunsWithPanic, j.id, j.name, recoverData)
// if panic is occurred, we should return an error
err = ErrPanicRecovered
err = fmt.Errorf("%w from %v", ErrPanicRecovered, recoverData)
}
}()

View File

@ -646,6 +646,7 @@ func TestJob_NextRuns(t *testing.T) {
func TestJob_PanicOccurred(t *testing.T) {
gotCh := make(chan any)
errCh := make(chan error)
s := newTestScheduler(t)
_, err := s.NewJob(
DurationJob(10*time.Millisecond),
@ -656,6 +657,8 @@ func TestJob_PanicOccurred(t *testing.T) {
WithEventListeners(
AfterJobRunsWithPanic(func(_ uuid.UUID, _ string, recoverData any) {
gotCh <- recoverData
}), AfterJobRunsWithError(func(_ uuid.UUID, _ string, err error) {
errCh <- err
}),
),
)
@ -665,6 +668,11 @@ func TestJob_PanicOccurred(t *testing.T) {
got := <-gotCh
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())
close(gotCh)
close(errCh)
}