-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcode.go
More file actions
88 lines (80 loc) · 3.49 KB
/
Copy pathcode.go
File metadata and controls
88 lines (80 loc) · 3.49 KB
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
package statusx
import (
statusv1 "github.com/qor5/x/v3/statusx/gen/status/v1"
"google.golang.org/grpc/codes"
)
// ReasonFromCode maps gRPC codes to their corresponding ErrorReason values
func ReasonFromCode(code codes.Code) statusv1.ErrorReason {
switch code {
case codes.OK:
return statusv1.ErrorReason_OK
case codes.Canceled:
return statusv1.ErrorReason_CANCELED
case codes.Unknown:
return statusv1.ErrorReason_UNKNOWN
case codes.InvalidArgument:
return statusv1.ErrorReason_INVALID_ARGUMENT
case codes.DeadlineExceeded:
return statusv1.ErrorReason_DEADLINE_EXCEEDED
case codes.NotFound:
return statusv1.ErrorReason_NOT_FOUND
case codes.AlreadyExists:
return statusv1.ErrorReason_ALREADY_EXISTS
case codes.PermissionDenied:
return statusv1.ErrorReason_PERMISSION_DENIED
case codes.ResourceExhausted:
return statusv1.ErrorReason_RESOURCE_EXHAUSTED
case codes.FailedPrecondition:
return statusv1.ErrorReason_FAILED_PRECONDITION
case codes.Aborted:
return statusv1.ErrorReason_ABORTED
case codes.OutOfRange:
return statusv1.ErrorReason_OUT_OF_RANGE
case codes.Unimplemented:
return statusv1.ErrorReason_UNIMPLEMENTED
case codes.Internal:
return statusv1.ErrorReason_INTERNAL
case codes.Unavailable:
return statusv1.ErrorReason_UNAVAILABLE
case codes.DataLoss:
return statusv1.ErrorReason_DATA_LOSS
case codes.Unauthenticated:
return statusv1.ErrorReason_UNAUTHENTICATED
default:
return statusv1.ErrorReason_UNKNOWN
}
}
// NewCode creates a Status with automatically derived reason from the gRPC code.
// This is a convenience function that uses ReasonFromCode to generate the reason.
func NewCode(code codes.Code, message string) *Status {
return New(code, ReasonFromCode(code).String(), message)
}
// NewCodef creates a Status with automatically derived reason and formatted message.
// This is a convenience function that uses ReasonFromCode to generate the reason.
func NewCodef(code codes.Code, format string, a ...any) *Status {
return Newf(code, ReasonFromCode(code).String(), format, a...)
}
// WrapCode wraps an error with automatically derived reason from the gRPC code.
// This is a convenience function that uses ReasonFromCode to generate the reason.
func WrapCode(err error, code codes.Code, message string) *Status {
return Wrap(err, code, ReasonFromCode(code).String(), message)
}
// WrapCodef wraps an error with automatically derived reason and formatted message.
// This is a convenience function that uses ReasonFromCode to generate the reason.
func WrapCodef(err error, code codes.Code, format string, a ...any) *Status {
return Wrapf(err, code, ReasonFromCode(code).String(), format, a...)
}
// AlwaysWrapCode is like WrapCode but always sets the given code and message,
// even if err is already a Status error. The original error is preserved as the cause.
// If err is nil, it returns an OK status. See AlwaysWrap for details on the
// codes.OK + non-nil error edge case.
func AlwaysWrapCode(err error, code codes.Code, message string) *Status {
return AlwaysWrap(err, code, ReasonFromCode(code).String(), message)
}
// AlwaysWrapCodef is like WrapCodef but always sets the given code and formatted message,
// even if err is already a Status error. The original error is preserved as the cause.
// If err is nil, it returns an OK status. See AlwaysWrap for details on the
// codes.OK + non-nil error edge case.
func AlwaysWrapCodef(err error, code codes.Code, format string, a ...any) *Status {
return AlwaysWrapf(err, code, ReasonFromCode(code).String(), format, a...)
}