All files / kernel-language-model-service/src/open-v1 response-json.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

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 28 29 30 31 32 33 34                  15x 15x 3x       12x                   9x 7x   2x 2x        
/**
 * Read the response body as UTF-8 text. Throws if {@link Response.ok} is false.
 *
 * @param response - The fetch response.
 * @returns The full response body text.
 */
export async function readAndCheckResponse(
  response: Response,
): Promise<string> {
  const body = await response.text();
  if (!response.ok) {
    throw new Error(
      `HTTP ${response.status} ${response.statusText} — ${body.slice(0, 500)}`,
    );
  }
  return body;
}
 
/**
 * If {@link Response.ok} is false, read the body as text and throw.
 * When OK, returns without reading the body so the stream remains available.
 *
 * @param response - The fetch response.
 */
export async function checkResponseOk(response: Response): Promise<void> {
  if (response.ok) {
    return;
  }
  const body = await response.text();
  throw new Error(
    `HTTP ${response.status} ${response.statusText} — ${body.slice(0, 500)}`,
  );
}