Use `errors.New` for non-formatted strings

This commit is contained in:
apocelipes 2025-09-02 18:04:18 +08:00
parent cc3a1dbf46
commit 8f12d0bec9
5 changed files with 69 additions and 67 deletions

112
errors.go
View File

@ -1,65 +1,67 @@
package gocron package gocron
import "fmt" import (
"errors"
)
// Public error definitions // Public error definitions
var ( var (
ErrCronJobInvalid = fmt.Errorf("gocron: CronJob: invalid crontab") ErrCronJobInvalid = errors.New("gocron: CronJob: invalid crontab")
ErrCronJobParse = fmt.Errorf("gocron: CronJob: crontab parse failure") ErrCronJobParse = errors.New("gocron: CronJob: crontab parse failure")
ErrDailyJobAtTimeNil = fmt.Errorf("gocron: DailyJob: atTime within atTimes must not be nil") ErrDailyJobAtTimeNil = errors.New("gocron: DailyJob: atTime within atTimes must not be nil")
ErrDailyJobAtTimesNil = fmt.Errorf("gocron: DailyJob: atTimes must not be nil") ErrDailyJobAtTimesNil = errors.New("gocron: DailyJob: atTimes must not be nil")
ErrDailyJobHours = fmt.Errorf("gocron: DailyJob: atTimes hours must be between 0 and 23 inclusive") ErrDailyJobHours = errors.New("gocron: DailyJob: atTimes hours must be between 0 and 23 inclusive")
ErrDailyJobZeroInterval = fmt.Errorf("gocron: DailyJob: interval must be greater than 0") ErrDailyJobZeroInterval = errors.New("gocron: DailyJob: interval must be greater than 0")
ErrDailyJobMinutesSeconds = fmt.Errorf("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") ErrDailyJobMinutesSeconds = errors.New("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
ErrDurationJobIntervalZero = fmt.Errorf("gocron: DurationJob: time interval is 0") ErrDurationJobIntervalZero = errors.New("gocron: DurationJob: time interval is 0")
ErrDurationRandomJobMinMax = fmt.Errorf("gocron: DurationRandomJob: minimum duration must be less than maximum duration") ErrDurationRandomJobMinMax = errors.New("gocron: DurationRandomJob: minimum duration must be less than maximum duration")
ErrEventListenerFuncNil = fmt.Errorf("gocron: eventListenerFunc must not be nil") ErrEventListenerFuncNil = errors.New("gocron: eventListenerFunc must not be nil")
ErrJobNotFound = fmt.Errorf("gocron: job not found") ErrJobNotFound = errors.New("gocron: job not found")
ErrJobRunNowFailed = fmt.Errorf("gocron: Job: RunNow: scheduler unreachable") ErrJobRunNowFailed = errors.New("gocron: Job: RunNow: scheduler unreachable")
ErrMonthlyJobDays = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must be between 31 and -31 inclusive, and not 0") ErrMonthlyJobDays = errors.New("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") ErrMonthlyJobAtTimeNil = errors.New("gocron: MonthlyJob: atTime within atTimes must not be nil")
ErrMonthlyJobAtTimesNil = fmt.Errorf("gocron: MonthlyJob: atTimes must not be nil") ErrMonthlyJobAtTimesNil = errors.New("gocron: MonthlyJob: atTimes must not be nil")
ErrMonthlyJobDaysNil = fmt.Errorf("gocron: MonthlyJob: daysOfTheMonth must not be nil") ErrMonthlyJobDaysNil = errors.New("gocron: MonthlyJob: daysOfTheMonth must not be nil")
ErrMonthlyJobHours = fmt.Errorf("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive") ErrMonthlyJobHours = errors.New("gocron: MonthlyJob: atTimes hours must be between 0 and 23 inclusive")
ErrMonthlyJobZeroInterval = fmt.Errorf("gocron: MonthlyJob: interval must be greater than 0") ErrMonthlyJobZeroInterval = errors.New("gocron: MonthlyJob: interval must be greater than 0")
ErrMonthlyJobMinutesSeconds = fmt.Errorf("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") ErrMonthlyJobMinutesSeconds = errors.New("gocron: MonthlyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
ErrNewJobTaskNil = fmt.Errorf("gocron: NewJob: Task must not be nil") ErrNewJobTaskNil = errors.New("gocron: NewJob: Task must not be nil")
ErrNewJobTaskNotFunc = fmt.Errorf("gocron: NewJob: Task.Function must be of kind reflect.Func") ErrNewJobTaskNotFunc = errors.New("gocron: NewJob: Task.Function must be of kind reflect.Func")
ErrNewJobWrongNumberOfParameters = fmt.Errorf("gocron: NewJob: Number of provided parameters does not match expected") ErrNewJobWrongNumberOfParameters = errors.New("gocron: NewJob: Number of provided parameters does not match expected")
ErrNewJobWrongTypeOfParameters = fmt.Errorf("gocron: NewJob: Type of provided parameters does not match expected") ErrNewJobWrongTypeOfParameters = errors.New("gocron: NewJob: Type of provided parameters does not match expected")
ErrOneTimeJobStartDateTimePast = fmt.Errorf("gocron: OneTimeJob: start must not be in the past") ErrOneTimeJobStartDateTimePast = errors.New("gocron: OneTimeJob: start must not be in the past")
ErrStopExecutorTimedOut = fmt.Errorf("gocron: timed out waiting for executor to stop") ErrStopExecutorTimedOut = errors.New("gocron: timed out waiting for executor to stop")
ErrStopJobsTimedOut = fmt.Errorf("gocron: timed out waiting for jobs to finish") ErrStopJobsTimedOut = errors.New("gocron: timed out waiting for jobs to finish")
ErrStopSchedulerTimedOut = fmt.Errorf("gocron: timed out waiting for scheduler to stop") ErrStopSchedulerTimedOut = errors.New("gocron: timed out waiting for scheduler to stop")
ErrWeeklyJobAtTimeNil = fmt.Errorf("gocron: WeeklyJob: atTime within atTimes must not be nil") ErrWeeklyJobAtTimeNil = errors.New("gocron: WeeklyJob: atTime within atTimes must not be nil")
ErrWeeklyJobAtTimesNil = fmt.Errorf("gocron: WeeklyJob: atTimes must not be nil") ErrWeeklyJobAtTimesNil = errors.New("gocron: WeeklyJob: atTimes must not be nil")
ErrWeeklyJobDaysOfTheWeekNil = fmt.Errorf("gocron: WeeklyJob: daysOfTheWeek must not be nil") ErrWeeklyJobDaysOfTheWeekNil = errors.New("gocron: WeeklyJob: daysOfTheWeek must not be nil")
ErrWeeklyJobHours = fmt.Errorf("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive") ErrWeeklyJobHours = errors.New("gocron: WeeklyJob: atTimes hours must be between 0 and 23 inclusive")
ErrWeeklyJobZeroInterval = fmt.Errorf("gocron: WeeklyJob: interval must be greater than 0") ErrWeeklyJobZeroInterval = errors.New("gocron: WeeklyJob: interval must be greater than 0")
ErrWeeklyJobMinutesSeconds = fmt.Errorf("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive") ErrWeeklyJobMinutesSeconds = errors.New("gocron: WeeklyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
ErrPanicRecovered = fmt.Errorf("gocron: panic recovered") ErrPanicRecovered = errors.New("gocron: panic recovered")
ErrWithClockNil = fmt.Errorf("gocron: WithClock: clock must not be nil") ErrWithClockNil = errors.New("gocron: WithClock: clock must not be nil")
ErrWithContextNil = fmt.Errorf("gocron: WithContext: context must not be nil") ErrWithContextNil = errors.New("gocron: WithContext: context must not be nil")
ErrWithDistributedElectorNil = fmt.Errorf("gocron: WithDistributedElector: elector must not be nil") ErrWithDistributedElectorNil = errors.New("gocron: WithDistributedElector: elector must not be nil")
ErrWithDistributedLockerNil = fmt.Errorf("gocron: WithDistributedLocker: locker must not be nil") ErrWithDistributedLockerNil = errors.New("gocron: WithDistributedLocker: locker must not be nil")
ErrWithDistributedJobLockerNil = fmt.Errorf("gocron: WithDistributedJobLocker: locker must not be nil") ErrWithDistributedJobLockerNil = errors.New("gocron: WithDistributedJobLocker: locker must not be nil")
ErrWithIdentifierNil = fmt.Errorf("gocron: WithIdentifier: identifier must not be nil") ErrWithIdentifierNil = errors.New("gocron: WithIdentifier: identifier must not be nil")
ErrWithLimitConcurrentJobsZero = fmt.Errorf("gocron: WithLimitConcurrentJobs: limit must be greater than 0") ErrWithLimitConcurrentJobsZero = errors.New("gocron: WithLimitConcurrentJobs: limit must be greater than 0")
ErrWithLocationNil = fmt.Errorf("gocron: WithLocation: location must not be nil") ErrWithLocationNil = errors.New("gocron: WithLocation: location must not be nil")
ErrWithLoggerNil = fmt.Errorf("gocron: WithLogger: logger must not be nil") ErrWithLoggerNil = errors.New("gocron: WithLogger: logger must not be nil")
ErrWithMonitorNil = fmt.Errorf("gocron: WithMonitor: monitor must not be nil") ErrWithMonitorNil = errors.New("gocron: WithMonitor: monitor must not be nil")
ErrWithNameEmpty = fmt.Errorf("gocron: WithName: name must not be empty") ErrWithNameEmpty = errors.New("gocron: WithName: name must not be empty")
ErrWithStartDateTimePast = fmt.Errorf("gocron: WithStartDateTime: start must not be in the past") ErrWithStartDateTimePast = errors.New("gocron: WithStartDateTime: start must not be in the past")
ErrWithStopDateTimePast = fmt.Errorf("gocron: WithStopDateTime: end must not be in the past") ErrWithStopDateTimePast = errors.New("gocron: WithStopDateTime: end must not be in the past")
ErrStartTimeLaterThanEndTime = fmt.Errorf("gocron: WithStartDateTime: start must not be later than end") ErrStartTimeLaterThanEndTime = errors.New("gocron: WithStartDateTime: start must not be later than end")
ErrStopTimeEarlierThanStartTime = fmt.Errorf("gocron: WithStopDateTime: end must not be earlier than start") ErrStopTimeEarlierThanStartTime = errors.New("gocron: WithStopDateTime: end must not be earlier than start")
ErrWithStopTimeoutZeroOrNegative = fmt.Errorf("gocron: WithStopTimeout: timeout must be greater than 0") ErrWithStopTimeoutZeroOrNegative = errors.New("gocron: WithStopTimeout: timeout must be greater than 0")
) )
// internal errors // internal errors
var ( var (
errAtTimeNil = fmt.Errorf("errAtTimeNil") errAtTimeNil = errors.New("errAtTimeNil")
errAtTimesNil = fmt.Errorf("errAtTimesNil") errAtTimesNil = errors.New("errAtTimesNil")
errAtTimeHours = fmt.Errorf("errAtTimeHours") errAtTimeHours = errors.New("errAtTimeHours")
errAtTimeMinSec = fmt.Errorf("errAtTimeMinSec") errAtTimeMinSec = errors.New("errAtTimeMinSec")
) )

View File

@ -2,6 +2,7 @@ package gocron_test
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sync" "sync"
"time" "time"
@ -58,7 +59,7 @@ var _ gocron.Locker = new(errorLocker)
type errorLocker struct{} type errorLocker struct{}
func (e errorLocker) Lock(_ context.Context, _ string) (gocron.Lock, error) { func (e errorLocker) Lock(_ context.Context, _ string) (gocron.Lock, error) {
return nil, fmt.Errorf("locked") return nil, errors.New("locked")
} }
func ExampleAfterLockError() { func ExampleAfterLockError() {
@ -120,7 +121,7 @@ func ExampleBeforeJobRunsSkipIfBeforeFuncErrors() {
gocron.WithEventListeners( gocron.WithEventListeners(
gocron.BeforeJobRunsSkipIfBeforeFuncErrors( gocron.BeforeJobRunsSkipIfBeforeFuncErrors(
func(jobID uuid.UUID, jobName string) error { func(jobID uuid.UUID, jobName string) error {
return fmt.Errorf("error") return errors.New("error")
}, },
), ),
), ),

View File

@ -2,7 +2,7 @@ package main
import ( import (
"context" "context"
"fmt" "errors"
"log" "log"
"time" "time"
@ -22,7 +22,7 @@ func (m myElector) IsLeader(_ context.Context) error {
return nil return nil
} }
log.Printf("node %d is not leader", m.num) log.Printf("node %d is not leader", m.num)
return fmt.Errorf("not leader") return errors.New("not leader")
} }
func main() { func main() {

View File

@ -3,7 +3,6 @@ package gocron
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"os" "os"
"sync" "sync"
@ -1561,7 +1560,7 @@ type testElector struct {
func (t *testElector) IsLeader(ctx context.Context) error { func (t *testElector) IsLeader(ctx context.Context) error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return fmt.Errorf("done") return errors.New("done")
default: default:
} }
@ -1569,7 +1568,7 @@ func (t *testElector) IsLeader(ctx context.Context) error {
defer t.mu.Unlock() defer t.mu.Unlock()
if t.leaderElected { if t.leaderElected {
t.notLeader <- struct{}{} t.notLeader <- struct{}{}
return fmt.Errorf("already elected leader") return errors.New("already elected leader")
} }
t.leaderElected = true t.leaderElected = true
return nil return nil
@ -1588,7 +1587,7 @@ func (t *testLocker) Lock(_ context.Context, _ string) (Lock, error) {
defer t.mu.Unlock() defer t.mu.Unlock()
if t.jobLocked { if t.jobLocked {
t.notLocked <- struct{}{} t.notLocked <- struct{}{}
return nil, fmt.Errorf("job already locked") return nil, errors.New("job already locked")
} }
t.jobLocked = true t.jobLocked = true
return &testLock{}, nil return &testLock{}, nil
@ -1952,7 +1951,7 @@ func TestScheduler_WithEventListeners(t *testing.T) {
defer verifyNoGoroutineLeaks(t) defer verifyNoGoroutineLeaks(t)
listenerRunCh := make(chan error, 1) listenerRunCh := make(chan error, 1)
testErr := fmt.Errorf("test error") testErr := errors.New("test error")
tests := []struct { tests := []struct {
name string name string
tsk Task tsk Task

View File

@ -1,7 +1,7 @@
package gocron package gocron
import ( import (
"fmt" "errors"
"testing" "testing"
"time" "time"
@ -64,10 +64,10 @@ func TestCallJobFuncWithParams(t *testing.T) {
{ {
"function that returns an error", "function that returns an error",
func() error { func() error {
return fmt.Errorf("test error") return errors.New("test error")
}, },
nil, nil,
fmt.Errorf("test error"), errors.New("test error"),
}, },
{ {
"function that returns no error", "function that returns no error",