-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrormap.go
72 lines (68 loc) · 1.74 KB
/
errormap.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 errormap
import (
"context"
"errors"
"net/http"
"github.com/cardboardrobots/baseerror"
"google.golang.org/grpc/codes"
)
func GetStatusCodeGRPC(err error) codes.Code {
if errors.Is(err, context.Canceled) {
return codes.Canceled
}
if errors.Is(err, context.DeadlineExceeded) {
return codes.DeadlineExceeded
}
if errors.Is(err, baseerror.ErrAlreadyExists) {
return codes.AlreadyExists
}
if errors.Is(err, baseerror.ErrFailedPrecondition) {
return codes.FailedPrecondition
}
if errors.Is(err, baseerror.ErrInvalidArgument) {
return codes.InvalidArgument
}
if errors.Is(err, baseerror.ErrNotFound) {
return codes.NotFound
}
if errors.Is(err, baseerror.ErrOutOfRange) {
return codes.OutOfRange
}
if errors.Is(err, baseerror.ErrPermissionDenied) {
return codes.PermissionDenied
}
if errors.Is(err, baseerror.ErrUnauthenticated) {
return codes.Unauthenticated
}
return codes.Unknown
}
func GetStatusCodeHTTP(err error) int {
if errors.Is(err, context.Canceled) {
return http.StatusInternalServerError
}
if errors.Is(err, context.DeadlineExceeded) {
return http.StatusInternalServerError
}
if errors.Is(err, baseerror.ErrAlreadyExists) {
return http.StatusNotModified
}
if errors.Is(err, baseerror.ErrFailedPrecondition) {
return http.StatusPreconditionFailed
}
if errors.Is(err, baseerror.ErrInvalidArgument) {
return http.StatusBadRequest
}
if errors.Is(err, baseerror.ErrNotFound) {
return http.StatusNotFound
}
if errors.Is(err, baseerror.ErrOutOfRange) {
return http.StatusInternalServerError
}
if errors.Is(err, baseerror.ErrPermissionDenied) {
return http.StatusForbidden
}
if errors.Is(err, baseerror.ErrUnauthenticated) {
return http.StatusUnauthorized
}
return http.StatusInternalServerError
}