All files / src/util apiError.ts

100% Statements 9/9
100% Branches 15/15
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                105x 19x   19x 10x 3x 3x   8x    
/**
 * Read the backend's message off a failed request.
 *
 * Every svc-core validation failure answers with a FastAPI ``detail`` naming
 * exactly what is wrong ("Variant 'X' has no model configured") — far more
 * useful to show than a generic "request failed".
 *
 * A structured detail carrying its own `message` is read too — a refusal that
 * ships the evidence with it (the promotion gate answers 409 with both the
 * sentence and the comparison that closed it) should still render its sentence
 * rather than falling back to "Request failed with status code 409".
 *
 * Anything else (the list shape FastAPI uses for request-body validation
 * errors) falls through to the axios message, so the caller never renders
 * "[object Object]".
 */
export const errorDetail = (e: unknown, fallback: string): string => {
  const detail = (e as { response?: { data?: { detail?: unknown } } })?.response
    ?.data?.detail;
  if (typeof detail === "string" && detail.trim()) return detail;
  if (detail && typeof detail === "object" && !Array.isArray(detail)) {
    const message = (detail as { message?: unknown }).message;
    if (typeof message === "string" && message.trim()) return message;
  }
  return (e as { message?: string })?.message?.trim() || fallback;
};