Open History to see the last commit
1import { NETWORK_BLOCKING_HINT } from "./network.ts";
2
3const escapeRegExp = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
4
5const TRANSPORT_ERROR_PATTERNS = [
6 /\bSocketCloseError\b/i,
7 /\bSocketOpenError\b/i,
8 /\bSocket is not connected\b/i,
9 /Unable to connect to the T3 server WebSocket\./i,
10 // The RPC session appends the network hint for relay connections. Any other
11 // trailing text means a different error that the user should still see.
12 new RegExp(
13 `\\b(?:is not connected|disconnected|stopped responding|could not establish a WebSocket connection)\\.(?: ${escapeRegExp(NETWORK_BLOCKING_HINT)})?$`,
14 "i",
15 ),
16 /\bClientProtocolError\b/i,
17 /\bRpcClientError\b/i,
18 /\bping timeout\b/i,
19] as const;
20
21export function isTransportConnectionErrorMessage(message: string | null | undefined): boolean {
22 if (typeof message !== "string") {
23 return false;
24 }
25
26 const normalizedMessage = message.trim();
27 if (normalizedMessage.length === 0) {
28 return false;
29 }
30
31 return TRANSPORT_ERROR_PATTERNS.some((pattern) => pattern.test(normalizedMessage));
32}
33
34export function sanitizeThreadErrorMessage(message: string | null | undefined): string | null {
35 return isTransportConnectionErrorMessage(message) ? null : (message ?? null);
36}