File size: 1,776 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { ErrorWithMetadata } from "./types";

export function upstreamErrorMessage(error: unknown): string {
	const candidate = error as { message?: unknown } | null | undefined;
	return String(candidate?.message || error);
}

export function upstreamErrorCode(error: unknown): string | undefined {
	const candidate = error as Partial<ErrorWithMetadata> | null | undefined;
	return candidate && typeof candidate.code === "string"
		? candidate.code
		: undefined;
}

export function upstreamErrorStatus(error: unknown): number | undefined {
	const candidate = error as Partial<ErrorWithMetadata> | null | undefined;
	const status = Number(candidate?.status);
	return Number.isInteger(status) && status >= 400 && status <= 599
		? status
		: undefined;
}

export function errorLogSummary(error: unknown): string {
	const candidate = error as Partial<ErrorWithMetadata> | null | undefined;
	const parts = [
		`type=${candidate && typeof candidate.name === "string" && candidate.name ? candidate.name : typeof error}`,
	];
	const code = upstreamErrorCode(error);
	if (code) parts.push(`code=${code}`);
	const status = upstreamErrorStatus(error);
	if (status) parts.push(`status=${status}`);
	const upstreamStatus = Number(candidate?.upstreamStatus);
	if (
		Number.isInteger(upstreamStatus) &&
		upstreamStatus >= 100 &&
		upstreamStatus <= 599
	)
		parts.push(`upstreamStatus=${upstreamStatus}`);
	const rawLength = Number(candidate?.rawLength);
	if (Number.isInteger(rawLength) && rawLength >= 0)
		parts.push(`rawLength=${rawLength}`);
	return parts.join(" ");
}

export function canFallbackAfterSocketError(
	_method: string,
	error: unknown,
): boolean {
	return !(
		error &&
		typeof error === "object" &&
		(error as Partial<ErrorWithMetadata>).upstreamStatus
	);
}