/
limiterror.go
88 lines (78 loc) · 2.16 KB
/
limiterror.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package errutil
import (
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/target/goalert/limit"
"github.com/target/goalert/util/sqlutil"
)
// LimitError represents an error caused by a configured system limit.
type LimitError interface {
error
Max() int
ID() limit.ID
Limit() bool
}
type limitErr struct {
id limit.ID
max int
}
var _ LimitError = &limitErr{}
// IsLimitError will determine if an error's cause is a limit.Error.
func IsLimitError(err error) bool {
if e, ok := errors.Cause(err).(LimitError); ok && e.Limit() {
return true
}
return false
}
func mapLimitError(s *sqlutil.Error) LimitError {
if s == nil {
return nil
}
if !strings.HasPrefix(s.Hint, "max=") {
return nil
}
if !strings.HasSuffix(s.ConstraintName, "_limit") {
return nil
}
id := limit.ID(strings.TrimSuffix(s.ConstraintName, "_limit"))
if id.Valid() != nil {
return nil
}
m, err := strconv.Atoi(strings.TrimPrefix(s.Hint, "max="))
if err != nil {
return nil
}
return &limitErr{id: id, max: m}
}
func (l *limitErr) ClientError() bool { return true }
func (l *limitErr) Limit() bool { return true }
func (l *limitErr) ID() limit.ID { return l.id }
func (l *limitErr) Max() int { return l.max }
func (l *limitErr) Error() string {
switch l.id {
case limit.NotificationRulesPerUser:
return "too many notification rules"
case limit.ContactMethodsPerUser:
return "too many contact methods"
case limit.EPStepsPerPolicy:
return "too many steps on this policy"
case limit.EPActionsPerStep:
return "too many actions on this step"
case limit.ParticipantsPerRotation:
return "too many participants on this rotation"
case limit.RulesPerSchedule:
return "too many rules on this schedule"
case limit.IntegrationKeysPerService:
return "too many integration keys on this service"
case limit.UnackedAlertsPerService:
return "too many unacknowledged alerts for this service"
case limit.TargetsPerSchedule:
return "too many targets on this schedule"
case limit.HeartbeatMonitorsPerService:
return "too many heartbeat monitors on this service"
case limit.UserOverridesPerSchedule:
return "too many user overrides on this schedule"
}
return "exceeded limit"
}