-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
59 lines (48 loc) · 1.28 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gocron
import (
"errors"
"fmt"
)
var (
ErrMalformedExpression = errors.New("expression is malformed")
ErrMalformedField = errors.New("unexpected field value")
ErrMultipleNotSpecified = errors.New("only one `?` is supported")
ErrValueOutsideRange = errors.New("values are outside the supported range")
)
// TimeUnitError is an error returned when a time unit of a Cron expression is
// malformed.
type TimeUnitError struct {
inner error
kind TimeUnitKind
}
func newTimeUnitErr(kind TimeUnitKind, inner error) TimeUnitError {
return TimeUnitError{inner: inner, kind: kind}
}
// Kind returns the time unit kind of the error (e.g. seconds).
func (e TimeUnitError) Kind() TimeUnitKind {
return e.kind
}
func (e TimeUnitError) Error() string {
return fmt.Sprintf("time unit `%s` malformed: %s", e.kind, e.inner)
}
func (e TimeUnitError) Is(err error) bool {
tue, ok := err.(TimeUnitError)
if ok {
return tue.kind == e.kind && errors.Is(e.inner, tue.inner)
}
return errors.Is(e.inner, err)
}
type TimeUnitKind int
const (
Seconds TimeUnitKind = iota
Minutes
Hours
Days
Months
WeekDays
Years
)
var kinds = []string{"seconds", "minutes", "hours", "days", "months", "week days", "years"}
func (k TimeUnitKind) String() string {
return kinds[int(k)%len(kinds)]
}