Detect non JSON BIM API responses

This commit is contained in:
CODEX 2026-06-20 19:08:22 +03:00
parent 2aab1bc9b4
commit b7fa6b5fbd
2 changed files with 14 additions and 2 deletions

View File

@ -3656,12 +3656,21 @@ const formatFetchFailureMessage = (url, res, text, data) => {
return (data && (data.message || data.error)) || text || `HTTP ${res.status}`;
};
const formatApiNonJsonMessage = (url, res, text) => {
const status = res?.status ? `HTTP ${res.status}` : "HTTP";
if (/^\s*<!doctype html/i.test(text || "")) {
return `BIM API на этом адресе вернул HTML вместо JSON (${status}). На localhost:8080 сейчас нет backend /api/comments.`;
}
return `BIM API вернул не-JSON ответ (${status}).`;
};
const fetchJSON = async (url, options = {}) => {
const apiRequest = isApiRequestUrl(url);
let res = null;
try {
res = await fetch(url, options);
} catch (err) {
if (isApiRequestUrl(url)) {
if (apiRequest) {
throw new Error(`BIM API недоступен: ${err?.message || "network error"}`);
}
throw err;
@ -3677,6 +3686,9 @@ const fetchJSON = async (url, options = {}) => {
const message = formatFetchFailureMessage(url, res, text, data);
throw new Error(message);
}
if (apiRequest && text && !data) {
throw new Error(formatApiNonJsonMessage(url, res, text));
}
return data;
};

View File

@ -570,6 +570,6 @@
<input id="fileInput" class="hidden" type="file"
accept=".xkt,.glb,.gltf,.bim,.las,.laz,.obj,.stl">
<script type="module" src="./dcViewer.js?v=36"></script>
<script type="module" src="./dcViewer.js?v=37"></script>
</body>
</html>