-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc_errors.go
77 lines (65 loc) · 2.62 KB
/
grpc_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
66
67
68
69
70
71
72
73
74
75
76
77
package backend_utils
import (
"google.golang.org/grpc/status"
"google.golang.org/grpc/codes"
)
/*
* gRPC error codes that are mapped to HTTP errors by grpc-gateway
* All the common error codes are exported from here
* as variables to avoid common function calls.
*
* grpc Error code | Http error status
* --------------------------------------------------------------------
* codes.OK | http.StatusOK
* codes.Canceled | http.StatusRequestTimeout
* codes.Unknown | http.StatusInternalServerError
* codes.InvalidArgument | http.StatusBadRequest
* codes.DeadlineExceeded | http.StatusRequestTimeout
* codes.NotFound | http.StatusNotFound
* codes.AlreadyExists | http.StatusConflict
* codes.PermissionDenied | http.StatusForbidden
* codes.Unauthenticated | http.StatusUnauthorized
* codes.ResourceExhausted | http.StatusForbidden
* codes.FailedPrecondition | http.StatusPreconditionFailed
* codes.Aborted | http.StatusConflict
* codes.OutOfRange | http.StatusBadRequest
* codes.Unimplemented | http.StatusNotImplemented
* codes.Internal | http.StatusInternalServerError
* codes.Unavailable | http.StatusServiceUnavailable
* codes.DataLoss | http.StatusInternalServerError
*
* Any other code will be defaulted to http.StatusInternalServerError
*/
// Not all of the above errors are implemented. Add them as and when required.
var (
ErrUnknown = func(msg string, args... interface{}) error {
return status.Errorf(codes.Unknown, msg, args...)
}
ErrInvalidArg = func(msg string, args... interface{}) error {
return status.Errorf(codes.InvalidArgument, msg, args...)
}
ErrNotFound = func(msg string, args... interface{}) error {
return status.Errorf(codes.NotFound, msg, args...)
}
ErrAlreadyExists = func(msg string, args... interface{}) error {
return status.Errorf(codes.AlreadyExists, msg, args...)
}
ErrResourceExhausted = func(msg string, args... interface{}) error {
return status.Errorf(codes.ResourceExhausted, msg, args...)
}
ErrPermissionDenied = func(msg string, args... interface{}) error {
return status.Errorf(codes.PermissionDenied, msg, args...)
}
ErrUnauthenticated = func(msg string, args... interface{}) error {
return status.Errorf(codes.Unauthenticated, msg, args...)
}
ErrInternal = func(msg string, args... interface{}) error {
return status.Errorf(codes.Internal, msg, args...)
}
ErrUnimplemented = func(msg string, args... interface{}) error {
return status.Errorf(codes.Unimplemented, msg, args...)
}
ErrUnavailable = func(msg string, args... interface{}) error {
return status.Errorf(codes.Unavailable, msg, args...)
}
)