forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
86 lines (71 loc) · 3.14 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
// 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 google_rpc status objects.
package status
import (
rpc "github.com/gogo/googleapis/google/rpc"
)
// 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)
}
// 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
}