diff --git a/livekit-rtc/livekit/rtc/participant.py b/livekit-rtc/livekit/rtc/participant.py index 9362a066..de7ba249 100644 --- a/livekit-rtc/livekit/rtc/participant.py +++ b/livekit-rtc/livekit/rtc/participant.py @@ -286,7 +286,7 @@ async def perform_rpc( FfiClient.instance.queue.unsubscribe(queue) if cb.perform_rpc.HasField("error"): - raise RpcError.from_proto(cb.perform_rpc.error) + raise RpcError._from_proto(cb.perform_rpc.error) return cb.perform_rpc.payload @@ -431,7 +431,7 @@ async def run_handler(): rpc_method_invocation_response=RpcMethodInvocationResponseRequest( local_participant_handle=self._ffi_handle.handle, invocation_id=invocation_id, - error=response_error.to_proto() if response_error else None, + error=response_error._to_proto() if response_error else None, payload=response_payload, ) ) diff --git a/livekit-rtc/livekit/rtc/rpc.py b/livekit-rtc/livekit/rtc/rpc.py index 771826a9..e7f4c98b 100644 --- a/livekit-rtc/livekit/rtc/rpc.py +++ b/livekit-rtc/livekit/rtc/rpc.py @@ -41,8 +41,8 @@ class RpcError(Exception): Instances of this type, when thrown in a method handler, will have their `message` serialized and sent across the wire. The caller will receive an equivalent error on the other side. - - Build-in types are included but developers may use any string, with a max length of 256 bytes. + + Built-in errors are included (codes 1001-1999) but developers may use the code, message, and data fields to create their own errors. """ class ErrorCode(IntEnum): @@ -83,27 +83,42 @@ def __init__( Creates an error object with the given code and message, plus an optional data payload. If thrown in an RPC method handler, the error will be sent back to the caller. - - Error codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings). + + Args: + code (int): Your error code (Error codes 1001-1999 are reserved for built-in errors) + message (str): A readable error message. + data (Optional[str]): Optional additional data associated with the error (JSON recommended) """ super().__init__(message) - self.code = code - self.message = message - self.data = data + self._code = code + self._message = message + self._data = data + + @property + def code(self) -> int: + """Error code value. Codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings).""" + return self._code + + @property + def message(self) -> str: + """A readable error message.""" + return self._message + + @property + def data(self) -> Optional[str]: + """Optional additional data associated with the error (JSON recommended).""" + return self._data @classmethod - def from_proto(cls, proto: proto_rpc.RpcError) -> "RpcError": + def _from_proto(cls, proto: proto_rpc.RpcError) -> "RpcError": return cls(proto.code, proto.message, proto.data) - def to_proto(self) -> proto_rpc.RpcError: + def _to_proto(self) -> proto_rpc.RpcError: return proto_rpc.RpcError(code=self.code, message=self.message, data=self.data) @classmethod def _built_in( cls, code: "RpcError.ErrorCode", data: Optional[str] = None ) -> "RpcError": - """ - Creates an error object from the ErrorCode, with an auto-populated message. - """ message = cls.ErrorMessage[code] return cls(code, message, data)