Skip to content

Commit

Permalink
Distinguish CORS errors from other network issues
Browse files Browse the repository at this point in the history
  • Loading branch information
moisseev committed Aug 18, 2024
1 parent 9d3991b commit c3a3c39
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@
"spamnessOptions.statusMessage.serverIsWritable": {
"message": "Server is writable."
},
"spamnessOptions.statusMessage.corsError": {
"message": "CORS error. Check the 'neighbours' list in the Rspamd options settings."
},
"spamnessOptions.statusMessage.requestCancelled": {
"message": "Request was cancelled."
},
Expand Down
3 changes: 3 additions & 0 deletions _locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
"spamnessOptions.statusMessage.serverIsWritable": {
"message": "Сервер доступен для записи."
},
"spamnessOptions.statusMessage.corsError": {
"message": "Ошибка CORS. Проверьте список 'neighbours' в настройках Rspamd."
},
"spamnessOptions.statusMessage.requestCancelled": {
"message": "Запрос был отменен."
},
Expand Down
14 changes: 13 additions & 1 deletion options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ async function checkServerStatus() {
}
}
} catch (error) {
if (error.name === "AbortError") {
if (error.message.includes("NetworkError") && await fetchWithNoCorsCheck()) {
updateStatusMessage("spamnessOptions.statusMessage.corsError");
} else if (error.name === "AbortError") {
updateStatusMessage("spamnessOptions.statusMessage.requestCancelled", "", "orange");
} else {
updateStatusMessage("spamnessOptions.statusMessage.errorCheckingServer", `${error.message}`);
Expand All @@ -200,6 +202,16 @@ async function checkServerStatus() {
loadingSpinner.classList.add("spinner-hidden");
document.querySelector("#check-server-status-button").disabled = false;
}

async function fetchWithNoCorsCheck() {
try {
await fetch(`${serverBaseUrl}/ping`, {mode: "no-cors"});
return true;
// eslint-disable-next-line no-unused-vars
} catch (_) {
return false;
}
}
}

function handleInputChange() {
Expand Down

0 comments on commit c3a3c39

Please sign in to comment.