Open History to see the last commit
1import { NETWORK_BLOCKING_HINT, UNRESPONSIVE_HOST_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 new RegExp(`\\bstopped responding for \\d+s\\. ${escapeRegExp(UNRESPONSIVE_HOST_HINT)}$`, "i"),
17 /\bClientProtocolError\b/i,
18 /\bRpcClientError\b/i,
19 /\bping timeout\b/i,
20] as const;
21
22export function isTransportConnectionErrorMessage(message: string | null | undefined): boolean {
23 if (typeof message !== "string") {
24 return false;
25 }
26
27 const normalizedMessage = message.trim();
28 if (normalizedMessage.length === 0) {
29 return false;
30 }
31
32 return TRANSPORT_ERROR_PATTERNS.some((pattern) => pattern.test(normalizedMessage));
33}
34
35export function sanitizeThreadErrorMessage(message: string | null | undefined): string | null {
36 return isTransportConnectionErrorMessage(message) ? null : (message ?? null);
37}