This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
119 lines (102 loc) · 4.41 KB
/
status.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
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package status provides utility functions for RPC status objects.
package status
import (
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
rpc "github.com/googleapis/googleapis/google/rpc"
me "github.com/hashicorp/go-multierror"
)
// OK represents a status with a code of rpc.OK
var OK = rpc.Status{Code: int32(rpc.OK)}
// New returns an initialized status with the given error code.
func New(c rpc.Code) rpc.Status {
return rpc.Status{Code: int32(c)}
}
// WithMessage returns an initialized status with the given error code and message
func WithMessage(c rpc.Code, message string) rpc.Status {
return rpc.Status{Code: int32(c), Message: message}
}
// WithError returns an initialized status with the rpc.INTERNAL error code and the error's message.
func WithError(err error) rpc.Status {
return rpc.Status{Code: int32(rpc.INTERNAL), Message: err.Error()}
}
// WithInternal returns an initialized status with the rpc.INTERNAL error code and the error's message.
func WithInternal(message string) rpc.Status {
return rpc.Status{Code: int32(rpc.INTERNAL), Message: message}
}
// WithCancelled returns an initialized status with the rpc.CANCELLED error code and the error's message.
func WithCancelled(message string) rpc.Status {
return rpc.Status{Code: int32(rpc.CANCELLED), Message: message}
}
// WithInvalidArgument returns an initialized status with the rpc.INVALID_ARGUMENT code and the given message.
func WithInvalidArgument(message string) rpc.Status {
return rpc.Status{Code: int32(rpc.INVALID_ARGUMENT), Message: message}
}
// WithPermissionDenied returns an initialized status with the rpc.PERMISSION_DENIED code and the given message.
func WithPermissionDenied(message string) rpc.Status {
return rpc.Status{Code: int32(rpc.PERMISSION_DENIED), Message: message}
}
// WithResourceExhausted returns an initialized status with the rpc.PERMISSION_DENIED code and the given message.
func WithResourceExhausted(message string) rpc.Status {
return rpc.Status{Code: int32(rpc.RESOURCE_EXHAUSTED), Message: message}
}
// WithDeadlineExceeded returns an initialized status with the rpc.DEADLINE_EXCEEDED code and the given message.
func WithDeadlineExceeded(message string) rpc.Status {
return rpc.Status{Code: int32(rpc.DEADLINE_EXCEEDED), Message: message}
}
// IsOK returns true is the given status has the code rpc.OK
func IsOK(status rpc.Status) bool {
return status.Code == int32(rpc.OK)
}
// InvalidWithDetails builds a google.rpc.Status proto with the provided
// message and the `details` field populated with the supplied proto message.
// NOTE: if there is an issue marshaling the proto to a google.protobuf.Any,
// the returned Status message will not have the `details` field populated.
func InvalidWithDetails(msg string, pb proto.Message) rpc.Status {
invalid := WithInvalidArgument(msg)
if any, err := types.MarshalAny(pb); err == nil {
invalid.Details = []*types.Any{any}
}
return invalid
}
// NewBadRequest builds a google.rpc.BadRequest proto. BadRequest proto messages
// can be used to populate the `details` field in a google.rpc.Status message.
func NewBadRequest(field string, err error) *rpc.BadRequest {
fvs := make([]*rpc.BadRequest_FieldViolation, 0, 1) // alloc for at least one
switch err.(type) {
case *me.Error:
merr := err.(*me.Error)
for _, e := range merr.Errors {
fv := &rpc.BadRequest_FieldViolation{Field: field, Description: e.Error()}
fvs = append(fvs, fv)
}
default:
fv := &rpc.BadRequest_FieldViolation{Field: field, Description: err.Error()}
fvs = append(fvs, fv)
}
return &rpc.BadRequest{FieldViolations: fvs}
}
// String produces a string representation of rpc.Status.
func String(status rpc.Status) string {
result, ok := rpc.Code_name[status.Code]
if !ok {
result = "Code" + string(status.Code)
}
if status.Message != "" {
result = result + " (" + status.Message + ")"
}
return result
}