pyRPC
DemoBlogChangelogDocs
Concepts

Error Handling

Structured errors from server to client.

Error Handling

pyRPC 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 pyrpc_core 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 (TypeScript)

Catch PyRPCError when calling procedures. The client ensures that server-side errors are caught and surfaced with their original codes and messages.

import { PyRPCError } from "@pyrpc/client";

try {
  const user = await client.get_user(999);
} catch (e) {
  if (e instanceof PyRPCError) {
    console.error(e.code);    // -404
    console.error(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