/** * RPC fetch errors — the ones a JSON-RPC client throws when an HTTP * request to a node fails — embed the full request URL (including any * `?token=`, and for some gateways a key in the URL *path*) in * multiple places on the error. Shape differs by library: * * - **ethers v6** nests it at `err.info.requestUrl` and on the * top-level `err.request`/`err.response` pair; `err.shortMessage` * is a safe summary ("server response 401 Unauthorized", "timeout", * etc.); `err.message` and `err.stack` echo the full URL in their * compound text. * - **ethers v5** exposes it at top-level `err.url`; `err.reason` is a * safe summary ("bad response", "missing response", etc.); * `err.message` and `err.stack` echo the full URL the same way. * - **viem** carries it on a plain `err.url` property, inside its * multi-line compound `message`, and in the `metaMessages` array * that its `BaseError` builds. Every wrapping viem error * (`ContractFunctionExecutionError`, `EstimateGasExecutionError`, * etc.) echoes the URL too because viem builds each parent's * `message` from the child's compound text. * * If an RPC call fails anywhere a service serialises the error to log * output, persisted state (Firestore, status routes), Sentry events, or * HTTP response bodies, the token lands wherever that data lands. The * leak is independent of which path the error reached: HTTP request * handler, cron tick, background consumer, `unhandledRejection`, * startup failure — anywhere errors go. * * Services often wrap the RPC error with a `VError`/`WError` for * contextual context ("Failed to fetch block number: "). The * sanitiser walks the `.cause` chain so the detection fires whether the * RPC error is the outermost throw or nested one or more levels deep, * and the full wrapping chain is preserved in the sanitised clone so * operators still see what the service was attempting. * * ## Redact, don't re-describe * * The sanitised clone keeps each library's **own field names and * structure** — same keys, scrubbed values. A consumer reads * `err.response.statusCode` on an ethers v6 error or `err.status` on a * viem one exactly as those libraries document, so nothing here invents * a parallel vocabulary that would have to be learned, kept in sync, and * mapped back. * * What forces the clone rather than in-place redaction is that the * hazardous members are **class instances**: ethers' `FetchRequest` * holds the tokenised URL, the auth headers and the request body, and * both it and `FetchResponse` keep everything in `#private` fields — so * they survive neither `JSON.stringify` nor a spread as anything but * `{}`, and their contents are reachable only through getters. viem's * `headers` is a fetch `Headers` instance with the same problem. Each is * therefore **projected** to a plain object under the same key with the * same sub-keys, carrying only the members that cannot hold a * credential. * * This projection is an **egress** step, applied where an error leaves * the process (the pino `err` serializer, `serializeError`, * `VError.toJSON`). In-process code keeps the genuine ethers/viem * instance with its full behaviour; only what gets written out is * projected. * * Lives in `@polygonlabs/verror` rather than `@polygonlabs/logger` * because it is an Error primitive — peer with `cause()`, `info()`, * `fullStack()` — not a logging concern. Logger was historically the * first consumer and remains a consumer (re-exporting for the existing * import path), but `serializeError` and `VError.toJSON` now invoke it * automatically so any persistence path (not just logs) gets URL * stripping by default. * * Detection is deliberately structural rather than `instanceof`-based, so * this package does not depend on ethers or viem. Extend the detectors * below when adopting a new RPC library with a similar fingerprint. */ /** * If `err` — or any error in its `.cause` chain — looks like an ethers v5, * ethers v6, or viem RPC fetch error, returns a sanitised clone of the * error: the full cause chain is preserved node-for-node with every * `message` and `stack` URL-stripped, the detected node's own fields * projected to scrubbed values under the library's own key names, and * intermediate `VError`/`WError` wrappers kept intact with their own * `info` and stack traces. Returns `null` for any other error shape, * letting the caller fall through to default handling. * * Because the projection keeps native names, a consumer that needs to * classify a failure it is about to retry — rate-limited vs upstream 5xx * vs an empty-bodied gateway timeout — reads the same fields the library * documents (`err.response.statusCode`, `err.status`, * `err.response.headers['retry-after']`) rather than fields invented * here, and needs no separate capture at the throw site. * * Preserving the chain matters: a service that wraps an RPC failure with * `new VError('failed to fetch block number', { cause: rpcErr })` logs a * full picture of "what was being attempted" and "why the RPC refused it". * Flattening the chain to a single RPC-only clone throws away the first * half — which is exactly the half operators reach for when debugging. * * @internal * * Prefer `serializeError` / `VError.toJSON` for any serialisation path — * they call this function at their entry, so logs, status routes, Sentry * events, Firestore documents, and `JSON.stringify(err)` are all safe by * default without the caller knowing this function exists. This export * is a building block for the narrow set of pipelines that need an * `Error`-in/`Error`-out sanitiser rather than a serialised `Record`: * the canonical example is `@polygonlabs/logger`'s pino `err` serializer, * which feeds the sanitised clone into pino's `stdSerializers.err` to * inherit pino's standard error shape. Adding a new direct call site is * a signal to review whether `serializeError` would do — almost always * the answer is yes. */ export declare function sanitiseRpcFetchError(err: unknown): Error | null; //# sourceMappingURL=sanitise.d.ts.map