From b383ca99c53ebd7d38ea4c602404c6f54beee01d Mon Sep 17 00:00:00 2001 From: apocelipes Date: Tue, 2 Sep 2025 21:48:40 +0800 Subject: [PATCH] Use `errors.New` for non-formatted strings (#870) --- errors.go | 112 ++++++++++++++++++++------------------- example_test.go | 5 +- examples/elector/main.go | 4 +- scheduler_test.go | 9 ++-- util_test.go | 6 +-- 5 files changed, 69 insertions(+), 67 deletions(-) diff --git a/errors.go b/errors.go index 93946e2..94c1f87 100644 --- a/errors.go +++ b/errors.go @@ -1,65 +1,67 @@ package gocron -import "fmt" +import ( + "errors" +) // Public error definitions var ( - ErrCronJobInvalid = fmt.Errorf("gocron: CronJob: invalid crontab") - ErrCronJobParse = fmt.Errorf("gocron: CronJob: crontab parse failure") - ErrDailyJobAtTimeNil = fmt.Errorf("gocron: DailyJob: atTime within atTimes must not be nil") - ErrDailyJobAtTimesNil = fmt.Errorf("gocron: DailyJob: atTimes must not be nil") - ErrDailyJobHours = fmt.Errorf("gocron: DailyJob: atTimes hours must be between 0 and 23 inclusive") - ErrDailyJobZeroInterval = fmt.Errorf("gocron: DailyJob: interval must be greater than 0") - ErrDailyJobMinutesSeconds = fmt.Errorf("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") - ErrDurationJobIntervalZero = fmt.Errorf("gocron: DurationJob: time interval is 0") - ErrDurationRandomJobMinMax = fmt.Errorf("gocron: DurationRandomJob: minimum duration must be less than maximum duration") - ErrEventListenerFuncNil = fmt.Errorf("gocron: eventListenerFunc must not be nil") - ErrJobNotFound = fmt.Errorf("gocron: job not found") - ErrJobRunNowFailed = fmt.Errorf("gocron: Job: RunNow: scheduler unreachable") - ErrMonthlyJobDays = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must be between 31 and -31 inclusive, and not 0") - ErrMonthlyJobAtTimeNil = fmt.Errorf("gocron: MonthlyJob: atTime within atTimes must not be nil") - ErrMonthlyJobAtTimesNil = fmt.Errorf("gocron: MonthlyJob: atTimes must not be nil") - ErrMonthlyJobDaysNil = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must not be nil") - ErrMonthlyJobHours = fmt.Errorf("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive") - ErrMonthlyJobZeroInterval = fmt.Errorf("gocron: MonthlyJob: interval must be greater than 0") - ErrMonthlyJobMinutesSeconds = fmt.Errorf("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") - ErrNewJobTaskNil = fmt.Errorf("gocron: NewJob: Task must not be nil") - ErrNewJobTaskNotFunc = fmt.Errorf("gocron: NewJob: Task.Function must be of kind reflect.Func") - ErrNewJobWrongNumberOfParameters = fmt.Errorf("gocron: NewJob: Number of provided parameters does not match expected") - ErrNewJobWrongTypeOfParameters = fmt.Errorf("gocron: NewJob: Type of provided parameters does not match expected") - ErrOneTimeJobStartDateTimePast = fmt.Errorf("gocron: OneTimeJob: start must not be in the past") - ErrStopExecutorTimedOut = fmt.Errorf("gocron: timed out waiting for executor to stop") - ErrStopJobsTimedOut = fmt.Errorf("gocron: timed out waiting for jobs to finish") - ErrStopSchedulerTimedOut = fmt.Errorf("gocron: timed out waiting for scheduler to stop") - ErrWeeklyJobAtTimeNil = fmt.Errorf("gocron: WeeklyJob: atTime within atTimes must not be nil") - ErrWeeklyJobAtTimesNil = fmt.Errorf("gocron: WeeklyJob: atTimes must not be nil") - ErrWeeklyJobDaysOfTheWeekNil = fmt.Errorf("gocron: WeeklyJob: daysOfTheWeek must not be nil") - ErrWeeklyJobHours = fmt.Errorf("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive") - ErrWeeklyJobZeroInterval = fmt.Errorf("gocron: WeeklyJob: interval must be greater than 0") - ErrWeeklyJobMinutesSeconds = fmt.Errorf("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") - ErrPanicRecovered = fmt.Errorf("gocron: panic recovered") - ErrWithClockNil = fmt.Errorf("gocron: WithClock: clock must not be nil") - ErrWithContextNil = fmt.Errorf("gocron: WithContext: context must not be nil") - ErrWithDistributedElectorNil = fmt.Errorf("gocron: WithDistributedElector: elector must not be nil") - ErrWithDistributedLockerNil = fmt.Errorf("gocron: WithDistributedLocker: locker must not be nil") - ErrWithDistributedJobLockerNil = fmt.Errorf("gocron: WithDistributedJobLocker: locker must not be nil") - ErrWithIdentifierNil = fmt.Errorf("gocron: WithIdentifier: identifier must not be nil") - ErrWithLimitConcurrentJobsZero = fmt.Errorf("gocron: WithLimitConcurrentJobs: limit must be greater than 0") - ErrWithLocationNil = fmt.Errorf("gocron: WithLocation: location must not be nil") - ErrWithLoggerNil = fmt.Errorf("gocron: WithLogger: logger must not be nil") - ErrWithMonitorNil = fmt.Errorf("gocron: WithMonitor: monitor must not be nil") - ErrWithNameEmpty = fmt.Errorf("gocron: WithName: name must not be empty") - ErrWithStartDateTimePast = fmt.Errorf("gocron: WithStartDateTime: start must not be in the past") - ErrWithStopDateTimePast = fmt.Errorf("gocron: WithStopDateTime: end must not be in the past") - ErrStartTimeLaterThanEndTime = fmt.Errorf("gocron: WithStartDateTime: start must not be later than end") - ErrStopTimeEarlierThanStartTime = fmt.Errorf("gocron: WithStopDateTime: end must not be earlier than start") - ErrWithStopTimeoutZeroOrNegative = fmt.Errorf("gocron: WithStopTimeout: timeout must be greater than 0") + ErrCronJobInvalid = errors.New("gocron: CronJob: invalid crontab") + ErrCronJobParse = errors.New("gocron: CronJob: crontab parse failure") + ErrDailyJobAtTimeNil = errors.New("gocron: DailyJob: atTime within atTimes must not be nil") + ErrDailyJobAtTimesNil = errors.New("gocron: DailyJob: atTimes must not be nil") + ErrDailyJobHours = errors.New("gocron: DailyJob: atTimes hours must be between 0 and 23 inclusive") + ErrDailyJobZeroInterval = errors.New("gocron: DailyJob: interval must be greater than 0") + ErrDailyJobMinutesSeconds = errors.New("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") + ErrDurationJobIntervalZero = errors.New("gocron: DurationJob: time interval is 0") + ErrDurationRandomJobMinMax = errors.New("gocron: DurationRandomJob: minimum duration must be less than maximum duration") + ErrEventListenerFuncNil = errors.New("gocron: eventListenerFunc must not be nil") + ErrJobNotFound = errors.New("gocron: job not found") + ErrJobRunNowFailed = errors.New("gocron: Job: RunNow: scheduler unreachable") + ErrMonthlyJobDays = errors.New("gocron: MonthlyJob: daysOfTheMonth must be between 31 and -31 inclusive, and not 0") + ErrMonthlyJobAtTimeNil = errors.New("gocron: MonthlyJob: atTime within atTimes must not be nil") + ErrMonthlyJobAtTimesNil = errors.New("gocron: MonthlyJob: atTimes must not be nil") + ErrMonthlyJobDaysNil = errors.New("gocron: MonthlyJob: daysOfTheMonth must not be nil") + ErrMonthlyJobHours = errors.New("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive") + ErrMonthlyJobZeroInterval = errors.New("gocron: MonthlyJob: interval must be greater than 0") + ErrMonthlyJobMinutesSeconds = errors.New("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") + ErrNewJobTaskNil = errors.New("gocron: NewJob: Task must not be nil") + ErrNewJobTaskNotFunc = errors.New("gocron: NewJob: Task.Function must be of kind reflect.Func") + ErrNewJobWrongNumberOfParameters = errors.New("gocron: NewJob: Number of provided parameters does not match expected") + ErrNewJobWrongTypeOfParameters = errors.New("gocron: NewJob: Type of provided parameters does not match expected") + ErrOneTimeJobStartDateTimePast = errors.New("gocron: OneTimeJob: start must not be in the past") + ErrStopExecutorTimedOut = errors.New("gocron: timed out waiting for executor to stop") + ErrStopJobsTimedOut = errors.New("gocron: timed out waiting for jobs to finish") + ErrStopSchedulerTimedOut = errors.New("gocron: timed out waiting for scheduler to stop") + ErrWeeklyJobAtTimeNil = errors.New("gocron: WeeklyJob: atTime within atTimes must not be nil") + ErrWeeklyJobAtTimesNil = errors.New("gocron: WeeklyJob: atTimes must not be nil") + ErrWeeklyJobDaysOfTheWeekNil = errors.New("gocron: WeeklyJob: daysOfTheWeek must not be nil") + ErrWeeklyJobHours = errors.New("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive") + ErrWeeklyJobZeroInterval = errors.New("gocron: WeeklyJob: interval must be greater than 0") + ErrWeeklyJobMinutesSeconds = errors.New("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") + ErrPanicRecovered = errors.New("gocron: panic recovered") + ErrWithClockNil = errors.New("gocron: WithClock: clock must not be nil") + ErrWithContextNil = errors.New("gocron: WithContext: context must not be nil") + ErrWithDistributedElectorNil = errors.New("gocron: WithDistributedElector: elector must not be nil") + ErrWithDistributedLockerNil = errors.New("gocron: WithDistributedLocker: locker must not be nil") + ErrWithDistributedJobLockerNil = errors.New("gocron: WithDistributedJobLocker: locker must not be nil") + ErrWithIdentifierNil = errors.New("gocron: WithIdentifier: identifier must not be nil") + ErrWithLimitConcurrentJobsZero = errors.New("gocron: WithLimitConcurrentJobs: limit must be greater than 0") + ErrWithLocationNil = errors.New("gocron: WithLocation: location must not be nil") + ErrWithLoggerNil = errors.New("gocron: WithLogger: logger must not be nil") + ErrWithMonitorNil = errors.New("gocron: WithMonitor: monitor must not be nil") + ErrWithNameEmpty = errors.New("gocron: WithName: name must not be empty") + ErrWithStartDateTimePast = errors.New("gocron: WithStartDateTime: start must not be in the past") + ErrWithStopDateTimePast = errors.New("gocron: WithStopDateTime: end must not be in the past") + ErrStartTimeLaterThanEndTime = errors.New("gocron: WithStartDateTime: start must not be later than end") + ErrStopTimeEarlierThanStartTime = errors.New("gocron: WithStopDateTime: end must not be earlier than start") + ErrWithStopTimeoutZeroOrNegative = errors.New("gocron: WithStopTimeout: timeout must be greater than 0") ) // internal errors var ( - errAtTimeNil = fmt.Errorf("errAtTimeNil") - errAtTimesNil = fmt.Errorf("errAtTimesNil") - errAtTimeHours = fmt.Errorf("errAtTimeHours") - errAtTimeMinSec = fmt.Errorf("errAtTimeMinSec") + errAtTimeNil = errors.New("errAtTimeNil") + errAtTimesNil = errors.New("errAtTimesNil") + errAtTimeHours = errors.New("errAtTimeHours") + errAtTimeMinSec = errors.New("errAtTimeMinSec") ) diff --git a/example_test.go b/example_test.go index ba5d721..bafc4d2 100644 --- a/example_test.go +++ b/example_test.go @@ -2,6 +2,7 @@ package gocron_test import ( "context" + "errors" "fmt" "sync" "time" @@ -58,7 +59,7 @@ var _ gocron.Locker = new(errorLocker) type errorLocker struct{} func (e errorLocker) Lock(_ context.Context, _ string) (gocron.Lock, error) { - return nil, fmt.Errorf("locked") + return nil, errors.New("locked") } func ExampleAfterLockError() { @@ -120,7 +121,7 @@ func ExampleBeforeJobRunsSkipIfBeforeFuncErrors() { gocron.WithEventListeners( gocron.BeforeJobRunsSkipIfBeforeFuncErrors( func(jobID uuid.UUID, jobName string) error { - return fmt.Errorf("error") + return errors.New("error") }, ), ), diff --git a/examples/elector/main.go b/examples/elector/main.go index bd05ad5..cbca152 100644 --- a/examples/elector/main.go +++ b/examples/elector/main.go @@ -2,7 +2,7 @@ package main import ( "context" - "fmt" + "errors" "log" "time" @@ -22,7 +22,7 @@ func (m myElector) IsLeader(_ context.Context) error { return nil } log.Printf("node %d is not leader", m.num) - return fmt.Errorf("not leader") + return errors.New("not leader") } func main() { diff --git a/scheduler_test.go b/scheduler_test.go index 3247698..115d170 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -3,7 +3,6 @@ package gocron import ( "context" "errors" - "fmt" "io" "os" "sync" @@ -1561,7 +1560,7 @@ type testElector struct { func (t *testElector) IsLeader(ctx context.Context) error { select { case <-ctx.Done(): - return fmt.Errorf("done") + return errors.New("done") default: } @@ -1569,7 +1568,7 @@ func (t *testElector) IsLeader(ctx context.Context) error { defer t.mu.Unlock() if t.leaderElected { t.notLeader <- struct{}{} - return fmt.Errorf("already elected leader") + return errors.New("already elected leader") } t.leaderElected = true return nil @@ -1588,7 +1587,7 @@ func (t *testLocker) Lock(_ context.Context, _ string) (Lock, error) { defer t.mu.Unlock() if t.jobLocked { t.notLocked <- struct{}{} - return nil, fmt.Errorf("job already locked") + return nil, errors.New("job already locked") } t.jobLocked = true return &testLock{}, nil @@ -1952,7 +1951,7 @@ func TestScheduler_WithEventListeners(t *testing.T) { defer verifyNoGoroutineLeaks(t) listenerRunCh := make(chan error, 1) - testErr := fmt.Errorf("test error") + testErr := errors.New("test error") tests := []struct { name string tsk Task diff --git a/util_test.go b/util_test.go index 05cc82d..f165d27 100644 --- a/util_test.go +++ b/util_test.go @@ -1,7 +1,7 @@ package gocron import ( - "fmt" + "errors" "testing" "time" @@ -64,10 +64,10 @@ func TestCallJobFuncWithParams(t *testing.T) { { "function that returns an error", func() error { - return fmt.Errorf("test error") + return errors.New("test error") }, nil, - fmt.Errorf("test error"), + errors.New("test error"), }, { "function that returns no error",