File size: 2,659 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
export type ApiErrorInfo = {
  message: string;
  traceId: string | null;
  /** Machine-readable `detail.code` from the backend, e.g. SUBSCRIPTION_REQUIRED. */
  code?: string | null;
  /** The full `detail` object, for codes that carry extra fields. */
  detail?: Record<string, unknown> | null;
};

/** Thrown by request helpers so callers can react to specific backend codes. */
export class ApiRequestError extends Error {
  readonly code: string | null;
  readonly detail: Record<string, unknown> | null;

  constructor(info: ApiErrorInfo) {
    super(formatSupportMessage(info));
    this.name = "ApiRequestError";
    this.code = info.code ?? null;
    this.detail = info.detail ?? null;
  }
}

export function isUpgradeErrorCode(code: string | null | undefined): boolean {
  return code === "SUBSCRIPTION_REQUIRED" || code === "VIDEO_TOO_LONG";
}

type ErrorPayload = {
  detail?: unknown;
  error?: unknown;
  message?: unknown;
  trace_id?: unknown;
};

function normalizeMessage(payload: ErrorPayload, fallback: string): string {
  if (typeof payload.message === "string" && payload.message.trim()) {
    return payload.message;
  }

  if (typeof payload.error === "string" && payload.error.trim()) {
    return payload.error;
  }

  if (typeof payload.detail === "string" && payload.detail.trim()) {
    return payload.detail;
  }

  if (
    payload.detail &&
    typeof payload.detail === "object" &&
    "message" in payload.detail &&
    typeof (payload.detail as { message?: unknown }).message === "string"
  ) {
    return (payload.detail as { message: string }).message;
  }

  return fallback;
}

export async function parseApiError(
  response: Response,
  fallbackMessage: string
): Promise<ApiErrorInfo> {
  const traceHeader = response.headers.get("x-trace-id");
  let payload: ErrorPayload = {};

  try {
    const data: unknown = await response.json();
    if (data && typeof data === "object") payload = data as ErrorPayload;
  } catch {
    // Some upstream responses are plain text
  }

  const traceFromBody =
    typeof payload.trace_id === "string" && payload.trace_id.trim()
      ? payload.trace_id
      : null;

  const detail =
    payload.detail && typeof payload.detail === "object"
      ? (payload.detail as Record<string, unknown>)
      : null;

  return {
    message: normalizeMessage(payload, fallbackMessage),
    traceId: traceHeader || traceFromBody,
    code: typeof detail?.code === "string" ? detail.code : null,
    detail,
  };
}

export function formatSupportMessage({ message, traceId }: ApiErrorInfo): string {
  if (!traceId) {
    return message;
  }

  return `${message} (Trace ID: ${traceId})`;
}