Detect non JSON BIM API responses
This commit is contained in:
parent
2aab1bc9b4
commit
b7fa6b5fbd
|
|
@ -3656,12 +3656,21 @@ const formatFetchFailureMessage = (url, res, text, data) => {
|
||||||
return (data && (data.message || data.error)) || text || `HTTP ${res.status}`;
|
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 fetchJSON = async (url, options = {}) => {
|
||||||
|
const apiRequest = isApiRequestUrl(url);
|
||||||
let res = null;
|
let res = null;
|
||||||
try {
|
try {
|
||||||
res = await fetch(url, options);
|
res = await fetch(url, options);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (isApiRequestUrl(url)) {
|
if (apiRequest) {
|
||||||
throw new Error(`BIM API недоступен: ${err?.message || "network error"}`);
|
throw new Error(`BIM API недоступен: ${err?.message || "network error"}`);
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
|
|
@ -3677,6 +3686,9 @@ const fetchJSON = async (url, options = {}) => {
|
||||||
const message = formatFetchFailureMessage(url, res, text, data);
|
const message = formatFetchFailureMessage(url, res, text, data);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
if (apiRequest && text && !data) {
|
||||||
|
throw new Error(formatApiNonJsonMessage(url, res, text));
|
||||||
|
}
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -570,6 +570,6 @@
|
||||||
<input id="fileInput" class="hidden" type="file"
|
<input id="fileInput" class="hidden" type="file"
|
||||||
accept=".xkt,.glb,.gltf,.bim,.las,.laz,.obj,.stl">
|
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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue