File size: 691 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export function parseErrorDetail(error: unknown): Record<string, any> | null {
  if (!(error instanceof Error)) return null;
  const msg = error.message;
  // Try " - " separator first (from request.ts formatted errors)
  const idx = msg.indexOf(" - ");
  if (idx !== -1) {
    try {
      const parsed = JSON.parse(msg.slice(idx + 3));
      return parsed?.detail || parsed;
    } catch {
      // fall through to raw JSON attempt
    }
  }
  // Fallback: try parsing the entire message as JSON
  try {
    const parsed = JSON.parse(msg);
    if (typeof parsed === "object" && parsed !== null) {
      return parsed?.detail || parsed;
    }
  } catch {
    // not JSON
  }
  return null;
}