Concepts

Error Handling

Structured errors from server to client.

Error Handling

pRPC uses structured errors that flow from your procedures to the client. No opaque strings — codes and messages are predictable.

Server-Side

Raise RPCError in your procedures:

from prpc import rpc, RPCError

@rpc
def get_user(user_id: int) -> dict:
    user = db.find_user(user_id)
    if not user:
        raise RPCError(-404, "User not found")
    return user

Client-Side

Catch RPCError when calling procedures:

from prpc import RPCClient, RPCError

with RPCClient("http://localhost:8000") as client:
    try:
        user = client.get_user(user_id=999)
    except RPCError as e:
        print(e.code)    # -404
        print(e.message) # User not found

Standard Codes

CodeMeaning
-32600Invalid Request
-32601Method not found
-32602Invalid params
-32603Internal error

Use negative codes (e.g. -404, -401) for application-specific errors.

Next

  • Server — Routers, context, authorization
  • Client — Sync, async, and TypeScript codegen