-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
200 lines (171 loc) · 7.31 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Package aphgrpc provides various interfaces, functions, types
// for building and working with gRPC services.
package aphgrpc
import (
"context"
"errors"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
const (
// MetaKey is the key used for storing all metadata
MetaKey = "error"
)
var (
//ErrDatabaseQuery represents database query related errors
ErrDatabaseQuery = newError("Database query error")
//ErrDatabaseInsert represents database insert related errors
ErrDatabaseInsert = newError("Database insert error")
//ErrDatabaseUpdate represents database update related errors
ErrDatabaseUpdate = newError("Database update error")
//ErrDatabaseDelete represents database update delete errors
ErrDatabaseDelete = newError("Database delete error")
//ErrNotFound represents the absence of an HTTP resource
ErrNotFound = newError("Resource not found")
//ErrExists represents the presence of an HTTP resource
ErrExists = newError("Resource already exists")
//ErrJSONEncoding represents any json encoding error
ErrJSONEncoding = newError("JSON encoding error")
//ErrStructMarshal represents any error with marshalling structure
ErrStructMarshal = newError("Structure marshalling error")
//ErrIncludeParam represents any error with invalid include query parameter
ErrIncludeParam = newErrorWithParam("Invalid include query parameter", "include")
//ErrSparseFieldSets represents any error with invalid sparse fieldsets query parameter
ErrFields = newErrorWithParam("Invalid field query parameter", "field")
//ErrFilterParam represents any error with invalid filter query paramter
ErrFilterParam = newErrorWithParam("Invalid filter query parameter", "filter")
//ErrNotAcceptable represents any error with wrong or inappropriate http Accept header
ErrNotAcceptable = newError("Accept header is not acceptable")
//ErrUnsupportedMedia represents any error with unsupported media type in http header
ErrUnsupportedMedia = newError("Media type is not supported")
//ErrInValidParam represents any error with validating input parameters
ErrInValidParam = newError("Invalid parameters")
//ErrRetrieveMetadata represents any error to retrieve grpc metadata from the running context
ErrRetrieveMetadata = errors.New("unable to retrieve metadata")
//ErrXForwardedHost represents any failure or absence of x-forwarded-host HTTP header in the grpc context
ErrXForwardedHost = errors.New("x-forwarded-host header is absent")
//ErrAuthentication represents the absence of valid authentication credentials
ErrAuthentication = newError("Invalid credentials for authentication")
//ErrMessagingReply represents any error in request reply messaging
ErrMessagingReply = newError("messaging reply error")
//ErrMessagingReq represents any error in request reply messaging
ErrMessagingReq = newError("messaging request error")
//ErrMessagingSub represents any error in publish subscribe messaging
ErrMessagingSub = newError("messaging subscription error")
//ErrMessagingPub represents any error in publish subscribe messaging
ErrMessagingPub = newError("messaging publication error")
//ErrOuthExchange represents any error in exchanging a code for a token with the oauth server
ErrOauthExchange = newError("Unable to exchange token for code")
//ErrUserRetrieval represents any error in retrieving user information from an oauth provider
ErrUserRetrieval = newError("Unable to retrieve user information")
)
func newErrorWithParam(msg, param string) metadata.MD {
return metadata.Pairs(MetaKey, msg, MetaKey, param)
}
func newError(msg string) metadata.MD {
return metadata.Pairs(MetaKey, msg)
}
func getgRPCStatus(err error) *status.Status {
s, ok := status.FromError(err)
if !ok {
return status.New(codes.Unknown, err.Error())
}
return s
}
func CheckNoRows(err error) bool {
if strings.Contains(err.Error(), "no rows") {
return true
}
return false
}
func HandleError(ctx context.Context, err error) error {
if CheckNoRows(err) {
grpc.SetTrailer(ctx, ErrNotFound)
return status.Error(codes.NotFound, err.Error())
}
grpc.SetTrailer(ctx, newError(err.Error()))
return status.Error(codes.Internal, err.Error())
}
func HandleGenericError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, newError(err.Error()))
return status.Error(codes.Internal, err.Error())
}
func HandleDeleteError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseDelete)
return status.Error(codes.Internal, err.Error())
}
func HandleGetError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseQuery)
return status.Error(codes.Internal, err.Error())
}
func HandleInsertError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseInsert)
return status.Error(codes.Internal, err.Error())
}
func HandleUpdateError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseUpdate)
return status.Error(codes.Internal, err.Error())
}
func HandleGetArgError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseQuery)
return status.Error(codes.InvalidArgument, err.Error())
}
func HandleInsertArgError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseInsert)
return status.Error(codes.InvalidArgument, err.Error())
}
func HandleUpdateArgError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrDatabaseUpdate)
return status.Error(codes.InvalidArgument, err.Error())
}
func HandleNotFoundError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrNotFound)
return status.Error(codes.NotFound, err.Error())
}
func HandleExistError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrExists)
return status.Error(codes.AlreadyExists, err.Error())
}
func HandleFilterParamError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrFilterParam)
return status.Error(codes.InvalidArgument, err.Error())
}
func HandleInvalidParamError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrInValidParam)
return status.Error(codes.InvalidArgument, err.Error())
}
func HandleAuthenticationError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrAuthentication)
return status.Error(codes.Unauthenticated, err.Error())
}
func HandleMessagingReplyError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrMessagingReply)
return status.Error(codes.Internal, err.Error())
}
func HandleMessagingReqError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrMessagingReq)
return status.Error(codes.Internal, err.Error())
}
func HandleMessagingSubError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrMessagingSub)
return status.Error(codes.Internal, err.Error())
}
func HandleMessagingPubError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrMessagingPub)
return status.Error(codes.Internal, err.Error())
}
func HandleOauthExchangeError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrOauthExchange)
return status.Error(codes.Internal, err.Error())
}
func HandleUserRetrievalError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrUserRetrieval)
return status.Error(codes.Internal, err.Error())
}
func HandleJSONEncodingError(ctx context.Context, err error) error {
grpc.SetTrailer(ctx, ErrJSONEncoding)
return status.Error(codes.Internal, err.Error())
}