Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/opencensus/trace/status.py: 86%

29 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-06 06:04 +0000

1# Copyright 2017, OpenCensus Authors 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15from google.rpc import code_pb2 

16 

17 

18class Status(object): 

19 """The Status type defines a logical error model that is suitable for 

20 different programming environments, including REST APIs and RPC APIs. 

21 It is used by gRPC. 

22 

23 :type code: int 

24 :param code: An enum value of :class: `~google.rpc.Code`. 

25 

26 :type message: str 

27 :param message: A developer-facing error message, should be in English. 

28 

29 :type details: list 

30 :param details: A list of messages that carry the error details. 

31 There is a common set of message types for APIs to use. 

32 e.g. [ 

33 { 

34 "@type": string, 

35 field1: ..., 

36 ... 

37 }, 

38 ] 

39 See: https://cloud.google.com/trace/docs/reference/v2/ 

40 rest/v2/Status#FIELDS.details 

41 """ 

42 def __init__(self, code, message=None, details=None): 

43 self.code = code 

44 self.message = message 

45 self.details = details 

46 

47 @property 

48 def canonical_code(self): 

49 return self.code 

50 

51 @property 

52 def description(self): 

53 return self.message 

54 

55 @property 

56 def is_ok(self): 

57 return self.canonical_code == code_pb2.OK 

58 

59 def format_status_json(self): 

60 """Convert a Status object to json format.""" 

61 status_json = {} 

62 

63 status_json['code'] = self.canonical_code 

64 

65 if self.description is not None: 

66 status_json['message'] = self.description 

67 

68 if self.details is not None: 

69 status_json['details'] = self.details 

70 

71 return status_json 

72 

73 @classmethod 

74 def from_exception(cls, exc): 

75 return cls( 

76 code=code_pb2.UNKNOWN, 

77 message=str(exc) 

78 ) 

79 

80 @classmethod 

81 def as_ok(cls): 

82 return cls( 

83 code=code_pb2.OK, 

84 )