Error Handling
Throw a typed error from a procedure. The client sees the message, not the stack.
Throw RpcError from a procedure or middleware for a custom JSON-RPC error. Plain Error becomes INTERNAL_ERROR with a generic message ("Internal error"). No stack or original message is leaked.
Security:
RpcError.datais serialized to the wire. Never put secrets, stack traces, or internal state in it.
import { const APP_ERROR_RANGE: {
readonly min: -32099;
readonly max: -32000;
}
APP_ERROR_RANGE, const JSON_RPC_ERROR: {
readonly PARSE_ERROR: -32700;
readonly INVALID_REQUEST: -32600;
readonly METHOD_NOT_FOUND: -32601;
readonly INVALID_PARAMS: -32602;
readonly INTERNAL_ERROR: -32603;
}
JSON_RPC_ERROR, class RpcErrorRpcError } from "tacho";
throw new new RpcError({ code, message, data }: {
code: number;
message: string;
data?: unknown;
}): RpcError
RpcError({
code: numbercode: -32001,
message: stringmessage: "nope",
data?: unknowndata: { retry: booleanretry: true },
});
const JSON_RPC_ERROR: {
readonly PARSE_ERROR: -32700;
readonly INVALID_REQUEST: -32600;
readonly METHOD_NOT_FOUND: -32601;
readonly INVALID_PARAMS: -32602;
readonly INTERNAL_ERROR: -32603;
}
JSON_RPC_ERROR.type PARSE_ERROR: -32700PARSE_ERROR;
const JSON_RPC_ERROR: {
readonly PARSE_ERROR: -32700;
readonly INVALID_REQUEST: -32600;
readonly METHOD_NOT_FOUND: -32601;
readonly INVALID_PARAMS: -32602;
readonly INTERNAL_ERROR: -32603;
}
JSON_RPC_ERROR.type INVALID_REQUEST: -32600INVALID_REQUEST;
const JSON_RPC_ERROR: {
readonly PARSE_ERROR: -32700;
readonly INVALID_REQUEST: -32600;
readonly METHOD_NOT_FOUND: -32601;
readonly INVALID_PARAMS: -32602;
readonly INTERNAL_ERROR: -32603;
}
JSON_RPC_ERROR.type METHOD_NOT_FOUND: -32601METHOD_NOT_FOUND;
const JSON_RPC_ERROR: {
readonly PARSE_ERROR: -32700;
readonly INVALID_REQUEST: -32600;
readonly METHOD_NOT_FOUND: -32601;
readonly INVALID_PARAMS: -32602;
readonly INTERNAL_ERROR: -32603;
}
JSON_RPC_ERROR.type INVALID_PARAMS: -32602INVALID_PARAMS;
const JSON_RPC_ERROR: {
readonly PARSE_ERROR: -32700;
readonly INVALID_REQUEST: -32600;
readonly METHOD_NOT_FOUND: -32601;
readonly INVALID_PARAMS: -32602;
readonly INTERNAL_ERROR: -32603;
}
JSON_RPC_ERROR.type INTERNAL_ERROR: -32603INTERNAL_ERROR;
const APP_ERROR_RANGE: {
readonly min: -32099;
readonly max: -32000;
}
APP_ERROR_RANGE;
rpcResult throws RpcError on a JSON-RPC error object. Clients check with instanceof RpcError.