Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherry committed Oct 30, 2024
1 parent 8481411 commit 2c3ba73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions livekit-rtc/livekit/rtc/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)
)
Expand Down
39 changes: 27 additions & 12 deletions livekit-rtc/livekit/rtc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 2c3ba73

Please sign in to comment.