Add better error handling

This commit is contained in:
Ivan Zinchenko 2024-09-25 17:26:52 +03:00
parent 2bf9a90901
commit 56cd21859a
Signed by: zinch
GPG Key ID: 6D45AA2C8FD6A37E
2 changed files with 34 additions and 21 deletions

View File

@ -31,7 +31,7 @@
<td colspan="5">
<div class="work-area">
<div class="window">
<div class="window" style="z-index: 90; left: 17px; top: 41px;">
<div class="window-title">Форма ввода</div>
<div class="window-body">
<form class="form" id="form">
@ -68,7 +68,7 @@
</div>
</div>
<div class="window">
<div class="window" style="z-index: 93; left: 89px; top: 416px;">
<div class="window-title">Ответ</div>
<div class="window-body">
<table class="table">
@ -87,7 +87,7 @@
</div>
</div>
<div class="window three-d">
<div class="window three-d" style="z-index: 91; left: 559px; top: 66px;">
<div class="window-title">areas.png</div>
<div class="window-body">
<img src="images/areas.png" alt="Мишень">

View File

@ -79,13 +79,13 @@ rButtons.forEach((button: HTMLButtonElement) => {
});
});
const form = document.querySelector('#form');
form.addEventListener('submit', e => {
e.preventDefault();
if (isNaN(request.y)) {
const showError = (errorText: string) => {
document.querySelector<HTMLAudioElement>('#error_sound').play();
const _window = document.createElement('div');
_window.classList.add('window');
_window.style.zIndex = String(500);
_window.style.left = "35%";
_window.style.top = "35%";
_window.innerHTML = `
<div class="window-title">
<span>Ошибка</span>
@ -94,11 +94,18 @@ form.addEventListener('submit', e => {
<div class="window-body">
<div class="error">
<img src="images/error.png" alt="Error Icon"/>
<p>Ошибка, проверьте, что Y это число</p>
<p>${errorText}</p>
</div>
</div>`;
windowDragHandler(_window);
document.querySelector<HTMLDivElement>('.work-area').append(_window);
}
const form = document.querySelector('#form');
form.addEventListener('submit', e => {
e.preventDefault();
if (isNaN(request.y)) {
showError('Ошибка, проверьте, что Y это число')
return;
}
@ -108,12 +115,18 @@ form.addEventListener('submit', e => {
r: String(request.r),
});
const response = fetch(`/ipa/cgi.jar?${params.toString()}`);
response.then(response => response.text().then(
response.then(response => {
if (response.status !== 200) {
showError("Ошибка при обработке запроса. Пожалуйста, попробуйте позже");
return;
}
response.text().then(
html => {
const element = document.createElement('tr');
element.innerHTML += html;
if (response.headers.get('X-Gol') === 'no') element.classList.add('grey');
document.querySelector("#results").append(element);
}
));
)
});
});