File size: 1,409 Bytes
461e15a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from "vitest";
import { formatStreamError } from "../src/agent/stream-errors.js";

describe("formatStreamError", () => {
  it("prefixes provider errors with their HTTP status", () => {
    const err = Object.assign(new Error("Payment Required"), { statusCode: 402 });
    expect(formatStreamError(err)).toBe("HTTP 402: Payment Required");
  });

  it("prefers the human message from a JSON response body", () => {
    const err = Object.assign(new Error("Bad"), {
      statusCode: 402,
      responseBody: JSON.stringify({ error: "You have exceeded your monthly included credits for Inference Providers." }),
    });
    expect(formatStreamError(err)).toBe(
      "HTTP 402: You have exceeded your monthly included credits for Inference Providers.",
    );
  });

  it("reads nested { error: { message } } bodies and the status of a wrapped cause", () => {
    const cause = Object.assign(new Error("x"), {
      statusCode: 429,
      responseBody: JSON.stringify({ error: { message: "Rate limit reached" } }),
    });
    const err = Object.assign(new Error("Rate limit reached"), { cause });
    expect(formatStreamError(err)).toBe("HTTP 429: Rate limit reached");
  });

  it("keeps plain errors unchanged", () => {
    expect(formatStreamError(new Error("socket hang up"))).toBe("socket hang up");
    expect(formatStreamError(undefined)).toBe("Stream error");
  });
});