/
maperror.go
72 lines (65 loc) · 2.6 KB
/
maperror.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
package errutil
import (
"strings"
"github.com/target/goalert/util/sqlutil"
"github.com/target/goalert/validation"
)
// MapDBError will map known DB errors (like unique names) to a valiation error
func MapDBError(err error) error {
if err == nil {
return nil
}
dbErr := sqlutil.MapError(err)
if dbErr == nil {
return err
}
switch dbErr.Code {
case "23503": // fkey constraint
switch dbErr.ConstraintName {
case "user_overrides_add_user_id_fkey":
return validation.NewFieldError("AddUserID", "user does not exist")
case "user_overrides_remove_user_id_fkey":
return validation.NewFieldError("RemoveUserID", "user does not exist")
case "user_overrides_tgt_schedule_id_fkey":
return validation.NewFieldError("TargetID", "schedule does not exist")
}
case "23505": // unique constraint
if strings.HasPrefix(dbErr.ConstraintName, dbErr.TableName+"_name") || dbErr.ConstraintName == "auth_basic_users_username_key" {
return validation.NewFieldError("Name", "already in use")
}
if dbErr.ConstraintName == "user_contact_methods_type_value_key" {
return validation.NewFieldError("Value", "contact method already exists for that type and value")
}
if dbErr.ConstraintName == "user_notification_rules_contact_method_id_delay_minutes_key" {
return validation.NewFieldError("DelayMinutes", "notification rule already exists for that delay and contact method")
}
if dbErr.ConstraintName == "heartbeat_monitor_name_service_id" {
return validation.NewFieldError("Name", "heartbeat monitor already exists with that name")
}
case "23514": // check constraint
newErr := mapLimitError(dbErr)
if newErr != nil {
return newErr
}
switch dbErr.ConstraintName {
case "user_overrides_check2":
return validation.NewFieldError("AddUserID", "cannot be the same as the user being replaced")
case "user_override_no_conflict_allowed":
return validation.NewFieldError("UserID", "cannot override the same user twice at the same time, check existing overrides; "+dbErr.Hint)
case "alert_status_user_id_match":
return validation.NewFieldError("AlertStatusCMID", "contact method is for wrong user")
case "notification_rule_user_id_match":
return validation.NewFieldError("UserID", "contact method is for wrong user")
}
}
switch dbErr.ConstraintName {
case "services_escalation_policy_id_fkey":
if strings.Contains(dbErr.Detail, "is still referenced") {
return validation.NewFieldError("EscalationPolicyID", "is currently in use")
}
if strings.Contains(dbErr.Detail, "is not present") {
return validation.NewFieldError("EscalationPolicyID", "does not exist")
}
}
return err
}