-
Notifications
You must be signed in to change notification settings - Fork 248
/
status.py
64 lines (53 loc) · 2.1 KB
/
status.py
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
# Copyright 2017, OpenCensus 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.
from google.rpc import code_pb2
class Status(object):
"""The Status type defines a logical error model that is suitable for
different programming environments, including REST APIs and RPC APIs.
It is used by gRPC.
:type code: int
:param code: An enum value of :class: `~google.rpc.Code`.
:type message: str
:param message: A developer-facing error message, should be in English.
:type details: list
:param details: A list of messages that carry the error details.
There is a common set of message types for APIs to use.
e.g. [
{
"@type": string,
field1: ...,
...
},
]
See: https://cloud.google.com/trace/docs/reference/v2/
rest/v2/Status#FIELDS.details
"""
def __init__(self, code, message, details=None):
self.code = code
self.message = message
self.details = details
def format_status_json(self):
"""Convert a Status object to json format."""
status_json = {}
status_json['code'] = self.code
status_json['message'] = self.message
if self.details is not None:
status_json['details'] = self.details
return status_json
@classmethod
def from_exception(cls, exc):
return cls(
code=code_pb2.UNKNOWN,
message=str(exc)
)