Skip to content

Commit

Permalink
wrap ErrPanicRecovered with recoverData (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
Higan authored Jul 3, 2024
1 parent 9d27ea8 commit 970c539
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gocron

import (
"context"
"fmt"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -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)
}
}()

Expand Down
8 changes: 8 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}),
),
)
Expand All @@ -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)
}

0 comments on commit 970c539

Please sign in to comment.