-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathexampleDebug.ts
More file actions
85 lines (79 loc) · 2.27 KB
/
exampleDebug.ts
File metadata and controls
85 lines (79 loc) · 2.27 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Shared debug helpers for mobile examples (session / op IDs, API request IDs).
*/
export function createSessionId(): string {
try {
const c = globalThis.crypto as Crypto | undefined
if (c?.randomUUID) {
return c.randomUUID().replace(/-/g, '').slice(0, 16)
}
} catch {
/* noop */
}
return `s${Math.random().toString(36).slice(2, 14)}`
}
export function newOperationId(): string {
try {
const c = globalThis.crypto as Crypto | undefined
if (c?.randomUUID) {
return c.randomUUID().replace(/-/g, '').slice(0, 8)
}
} catch {
/* noop */
}
return Math.random().toString(36).slice(2, 10)
}
/** Headers API gateways often attach — helps match client logs to server traces. */
export function extractServerRequestIds(res: Response): Record<string, string> {
const out: Record<string, string> = {}
const priority = [
'x-request-id',
'x-correlation-id',
'cf-ray',
'x-amzn-requestid',
'x-envoy-decorator-operation',
'traceparent'
]
for (const name of priority) {
const v = res.headers.get(name)
if (v) {
out[name] = v.trim()
}
}
res.headers.forEach((value, key) => {
if (/request.id|correlation|trace/i.test(key) && out[key] === undefined) {
out[key] = value.trim()
}
})
return out
}
export function primaryRequestId(ids: Record<string, string>): string | undefined {
return (
ids['x-request-id'] ??
ids['x-correlation-id'] ??
ids['cf-ray'] ??
Object.values(ids)[0]
)
}
export async function formatErrorForDebug(error: unknown): Promise<Record<string, unknown>> {
const base = {
message: error instanceof Error ? error.message : String(error)
} as Record<string, unknown>
if (error != null && typeof error === 'object' && 'response' in error) {
const response = (error as { response?: Response }).response
if (response != null) {
const serverRequestIds = extractServerRequestIds(response)
const rid = primaryRequestId(serverRequestIds)
const bodyText = await response.text().catch(() => '')
return {
...base,
status: response.status,
statusText: response.statusText,
bodyText,
serverRequestIds,
...(rid !== undefined ? { requestId: rid } : {})
}
}
}
return base
}