Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -5,6 +5,7 @@ import { readFile, writeFile, mkdir } from "node:fs/promises";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createHash } from "node:crypto";
|
||||
import { parseWeeksDocx, parseBellsDocx } from "./calendar.js";
|
||||
import { apiError, sourceError } from "./errors.js";
|
||||
import {
|
||||
academicStartYear,
|
||||
assignSemesters,
|
||||
@@ -68,6 +69,8 @@ export async function request(path, method = "GET") {
|
||||
encoding: "buffer",
|
||||
maxBuffer: 11 * 1024 * 1024,
|
||||
timeout: 28000,
|
||||
}).catch((error) => {
|
||||
throw sourceError(error);
|
||||
});
|
||||
return stdout;
|
||||
}
|
||||
@@ -164,7 +167,7 @@ async function discover(refresh = false) {
|
||||
root.filter((e) => e.folder).map((e) => nameOf(e.path)),
|
||||
);
|
||||
if (!year)
|
||||
throw new Error("В каталоге университета нет папки учебного года.");
|
||||
throw apiError(502, "source_invalid", "В каталоге университета нет папки учебного года.");
|
||||
const yearRoot = SHARE + encodeURIComponent(year.name) + "/";
|
||||
const yearEntries = decodeListing(
|
||||
(await request(yearRoot, "PROPFIND")).toString(),
|
||||
@@ -187,7 +190,8 @@ async function crawl() {
|
||||
files = [],
|
||||
count = 0;
|
||||
while (queue.length) {
|
||||
if (count + queue.length > 180) throw new Error("Каталог слишком большой.");
|
||||
if (count + queue.length > 180)
|
||||
throw apiError(502, "source_invalid", "Каталог университета слишком большой.");
|
||||
const batch = queue.splice(0, 5);
|
||||
count += batch.length;
|
||||
const results = await Promise.all(
|
||||
@@ -278,9 +282,7 @@ export async function documentFor(id) {
|
||||
const catalog = await getCatalog();
|
||||
const file = catalog.files.find((f) => f.id === id);
|
||||
if (!file) {
|
||||
const error = new Error("Документ не найден в каталоге. Обновите список.");
|
||||
error.status = 404;
|
||||
throw error;
|
||||
throw apiError(404, "document_not_found", "Документ не найден в каталоге. Обновите список.");
|
||||
}
|
||||
return file;
|
||||
}
|
||||
@@ -296,7 +298,7 @@ export function mergeWeekTables(tables) {
|
||||
async function loadCalendar() {
|
||||
const { references } = await discover(true);
|
||||
if (!references.weeks.length)
|
||||
throw new Error("Таблица белых и зелёных недель не найдена в каталоге.");
|
||||
throw apiError(502, "source_invalid", "Таблица белых и зелёных недель не найдена в каталоге.");
|
||||
const tables = [];
|
||||
for (const file of references.weeks)
|
||||
try {
|
||||
@@ -305,7 +307,7 @@ async function loadCalendar() {
|
||||
// One damaged copy must not hide a readable newer or older one.
|
||||
console.error(`${file.name}: ${error.message}`);
|
||||
}
|
||||
if (!tables.length) throw new Error("Таблица недель не распознана.");
|
||||
if (!tables.length) throw apiError(502, "source_invalid", "Таблица недель не распознана.");
|
||||
let bells = [];
|
||||
if (references.bells)
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user