Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.2 KB
Raw Blame
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { extractApiErrorMessage } from "@/shared/http/apiErrorMessage";

describe("extractApiErrorMessage (#5340)", () => {
  it("surfaces the message from a structured error envelope", () => {
    const body = {
      error: { code: "INVALID_ORIGIN", message: "Invalid request origin", correlation_id: "x" },
    };
    assert.equal(extractApiErrorMessage(body, "fallback"), "Invalid request origin");
  });

  it("returns a plain string error as-is", () => {
    assert.equal(extractApiErrorMessage({ error: "boom" }, "fallback"), "boom");
  });

  it("never renders a raw error object — falls back when message is missing", () => {
    assert.equal(extractApiErrorMessage({ error: { code: "X" } }, "fallback"), "fallback");
  });

  it("falls back for empty, null, or malformed bodies", () => {
    assert.equal(extractApiErrorMessage({ error: "  " }, "fallback"), "fallback");
    assert.equal(extractApiErrorMessage(null, "fallback"), "fallback");
    assert.equal(extractApiErrorMessage({}, "fallback"), "fallback");
    assert.equal(extractApiErrorMessage({ error: { message: 42 } }, "fallback"), "fallback");
  });
});