57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
|
|
// Tests for LabdApiError.
|
||
|
|
|
||
|
|
import { describe, it, expect } from "vitest";
|
||
|
|
import { LabdApiError, isLabdApiError } from "../src/api/errors.js";
|
||
|
|
|
||
|
|
describe("LabdApiError", () => {
|
||
|
|
it("constructs with status code and message", () => {
|
||
|
|
const err = new LabdApiError(404, "Not found");
|
||
|
|
expect(err.statusCode).toBe(404);
|
||
|
|
expect(err.message).toBe("Not found");
|
||
|
|
expect(err.errorCode).toBe("NOT_FOUND");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("fromResponse parses error body", () => {
|
||
|
|
const err = LabdApiError.fromResponse(400, {
|
||
|
|
error: "Invalid input",
|
||
|
|
detail: "hostname required",
|
||
|
|
});
|
||
|
|
expect(err.statusCode).toBe(400);
|
||
|
|
expect(err.message).toBe("Invalid input");
|
||
|
|
expect(err.detail).toBe("hostname required");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("fromResponse handles non-object body", () => {
|
||
|
|
const err = LabdApiError.fromResponse(500, "plain text");
|
||
|
|
expect(err.statusCode).toBe(500);
|
||
|
|
expect(err.message).toBe("HTTP 500");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("notConnected creates connection error", () => {
|
||
|
|
const err = LabdApiError.notConnected("https://localhost:8443");
|
||
|
|
expect(err.statusCode).toBe(0);
|
||
|
|
expect(err.errorCode).toBe("CONNECTION_ERROR");
|
||
|
|
expect(err.message).toContain("localhost:8443");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("timeout creates timeout error", () => {
|
||
|
|
const err = LabdApiError.timeout(30000);
|
||
|
|
expect(err.message).toContain("30000ms");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("isLabdApiError", () => {
|
||
|
|
it("returns true for LabdApiError", () => {
|
||
|
|
expect(isLabdApiError(new LabdApiError(500, "err"))).toBe(true);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("returns false for regular Error", () => {
|
||
|
|
expect(isLabdApiError(new Error("nope"))).toBe(false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("returns false for non-errors", () => {
|
||
|
|
expect(isLabdApiError(null)).toBe(false);
|
||
|
|
expect(isLabdApiError("string")).toBe(false);
|
||
|
|
});
|
||
|
|
});
|