@@ -41,8 +41,8 @@ class RpcError(Exception):
4141
4242 Instances of this type, when thrown in a method handler, will have their `message`
4343 serialized and sent across the wire. The caller will receive an equivalent error on the other side.
44-
45- Build -in types are included but developers may use any string, with a max length of 256 bytes .
44+
45+ Built -in errors are included (codes 1001-1999) but developers may use the code, message, and data fields to create their own errors .
4646 """
4747
4848 class ErrorCode (IntEnum ):
@@ -83,27 +83,42 @@ def __init__(
8383 Creates an error object with the given code and message, plus an optional data payload.
8484
8585 If thrown in an RPC method handler, the error will be sent back to the caller.
86-
87- Error codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings).
86+
87+ Args:
88+ code (int): Your error code (Error codes 1001-1999 are reserved for built-in errors)
89+ message (str): A readable error message.
90+ data (Optional[str]): Optional additional data associated with the error (JSON recommended)
8891 """
8992 super ().__init__ (message )
90- self .code = code
91- self .message = message
92- self .data = data
93+ self ._code = code
94+ self ._message = message
95+ self ._data = data
96+
97+ @property
98+ def code (self ) -> int :
99+ """Error code value. Codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings)."""
100+ return self ._code
101+
102+ @property
103+ def message (self ) -> str :
104+ """A readable error message."""
105+ return self ._message
106+
107+ @property
108+ def data (self ) -> Optional [str ]:
109+ """Optional additional data associated with the error (JSON recommended)."""
110+ return self ._data
93111
94112 @classmethod
95- def from_proto (cls , proto : proto_rpc .RpcError ) -> "RpcError" :
113+ def _from_proto (cls , proto : proto_rpc .RpcError ) -> "RpcError" :
96114 return cls (proto .code , proto .message , proto .data )
97115
98- def to_proto (self ) -> proto_rpc .RpcError :
116+ def _to_proto (self ) -> proto_rpc .RpcError :
99117 return proto_rpc .RpcError (code = self .code , message = self .message , data = self .data )
100118
101119 @classmethod
102120 def _built_in (
103121 cls , code : "RpcError.ErrorCode" , data : Optional [str ] = None
104122 ) -> "RpcError" :
105- """
106- Creates an error object from the ErrorCode, with an auto-populated message.
107- """
108123 message = cls .ErrorMessage [code ]
109124 return cls (code , message , data )
0 commit comments