29 lines
1.5 KiB
JavaScript
29 lines
1.5 KiB
JavaScript
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { apiError, describe, sourceError } from "../server/errors.js";
|
||
|
||
test("Ошибки API: код и статус для клиентов", () => {
|
||
assert.deepEqual(describe(apiError(404, "group_not_found", "В документе нет группы 1-А.")), {
|
||
status: 404,
|
||
code: "group_not_found",
|
||
message: "В документе нет группы 1-А.",
|
||
});
|
||
assert.deepEqual(describe({ code: "LIMIT_FILE_SIZE" }).code, "file_too_large");
|
||
// Faults of the server itself never show their technical text.
|
||
const internal = describe(new TypeError("Cannot read properties of undefined"));
|
||
assert.equal(internal.status, 500);
|
||
assert.equal(internal.code, "internal");
|
||
assert.doesNotMatch(internal.message, /undefined/);
|
||
});
|
||
|
||
test("Ошибки источника: таймаут отдельно от недоступности", () => {
|
||
const timeout = new DOMException("The operation was aborted due to timeout", "TimeoutError");
|
||
assert.equal(sourceError(timeout).code, "source_timeout");
|
||
assert.equal(sourceError(timeout).status, 504);
|
||
assert.equal(sourceError(new DOMException("aborted", "AbortError")).code, "source_timeout");
|
||
assert.equal(sourceError(new TypeError("fetch failed")).code, "source_unavailable");
|
||
assert.equal(sourceError(new Error("HTTP 500")).status, 502);
|
||
const own = apiError(502, "source_invalid", "Таблица недель не распознана.");
|
||
assert.equal(sourceError(own), own);
|
||
});
|