forked from brocaar/chirpstack-application-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
65 lines (56 loc) · 1.96 KB
/
errors.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
package storage
import (
"database/sql"
"github.com/lib/pq"
"github.com/pkg/errors"
)
// Action defines the action type.
type Action int
// Possible actions
const (
Select Action = iota
Insert
Update
Delete
Scan
)
// errors
var (
ErrAlreadyExists = errors.New("object already exists")
ErrDoesNotExist = errors.New("object does not exist")
ErrUsedByOtherObjects = errors.New("this object is used by other objects, remove them first")
ErrApplicationInvalidName = errors.New("invalid application name")
ErrNodeInvalidName = errors.New("invalid node name")
ErrNodeMaxRXDelay = errors.New("max value of RXDelay is 15")
ErrCFListTooManyChannels = errors.New("too many channels in channel-list")
ErrUserInvalidUsername = errors.New("username name may only be composed of upper and lower case characters and digits")
ErrUserPasswordLength = errors.New("passwords must be at least 6 characters long")
ErrInvalidUsernameOrPassword = errors.New("invalid username or password")
ErrOrganizationInvalidName = errors.New("invalid organization name")
ErrGatewayInvalidName = errors.New("invalid gateway name")
ErrInvalidEmail = errors.New("invalid e-mail")
ErrInvalidGatewayDiscoveryInterval = errors.New("invalid gateway-discovery interval, it must be greater than 0")
)
func handlePSQLError(action Action, err error, description string) error {
if err == sql.ErrNoRows {
return ErrDoesNotExist
}
switch err := err.(type) {
case *pq.Error:
switch err.Code.Name() {
case "unique_violation":
return ErrAlreadyExists
case "foreign_key_violation":
switch action {
case Delete:
return ErrUsedByOtherObjects
default:
return ErrDoesNotExist
}
}
}
return errors.Wrap(err, description)
}
func handleGrpcError(err error, description string) error {
return errors.Wrap(err, description)
}