Files
t3code/packages/client-runtime/src/errors/transport.ts
T

47 lines
1.6 KiB
TypeScript

import { NETWORK_BLOCKING_HINT } from "./network.ts";
const escapeRegExp = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const TRANSPORT_ERROR_PATTERNS = [
/\bSocketCloseError\b/i,
/\bSocketOpenError\b/i,
/\bSocket is not connected\b/i,
/Unable to connect to the T3 server WebSocket\./i,
// The RPC session appends the network hint for relay connections. Any other
// trailing text means a different error that the user should still see.
new RegExp(
`\\b(?:is not connected|disconnected|could not establish a WebSocket connection)\\.(?: ${escapeRegExp(NETWORK_BLOCKING_HINT)})?$`,
"i",
),
/\bClientProtocolError\b/i,
/\bRpcClientError\b/i,
/\bping timeout\b/i,
] as const;
/**
* Check whether an error message originates from a transport-level connection
* failure (socket close, socket open, ping timeout, etc.) rather than a
* business-logic error.
*/
export function isTransportConnectionErrorMessage(message: string | null | undefined): boolean {
if (typeof message !== "string") {
return false;
}
const normalizedMessage = message.trim();
if (normalizedMessage.length === 0) {
return false;
}
return TRANSPORT_ERROR_PATTERNS.some((pattern) => pattern.test(normalizedMessage));
}
/**
* Strip transport connection errors from user-facing error messages.
* Returns `null` for transport errors so the UI can distinguish between
* real errors and transient connection issues.
*/
export function sanitizeThreadErrorMessage(message: string | null | undefined): string | null {
return isTransportConnectionErrorMessage(message) ? null : (message ?? null);
}