add check for --api flag and helpful error message

Signed-off-by: Victor Seiji Hariki <victorseijih@gmail.com>
This commit is contained in:
Victor Seiji Hariki 2022-12-10 21:46:14 -03:00
parent 251a7191d3
commit 012f6ae272
2 changed files with 50 additions and 26 deletions

View file

@ -259,7 +259,7 @@ body {
color: #3f1f1f; color: #3f1f1f;
} }
.host-field-wrapper .connection-status.cors-issue { .host-field-wrapper .connection-status.webui-issue {
background-color: #dddd49; background-color: #dddd49;
color: #3f3f1f; color: #3f3f1f;
} }

View file

@ -176,7 +176,7 @@ async function testHostConnection() {
online: () => { online: () => {
connectionIndicator.classList.add("online"); connectionIndicator.classList.add("online");
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"cors-issue", "webui-issue",
"offline", "offline",
"before", "before",
"server-error" "server-error"
@ -191,7 +191,7 @@ async function testHostConnection() {
"online", "online",
"offline", "offline",
"before", "before",
"cors-issue" "webui-issue"
); );
connectionIndicatorText.textContent = "Error"; connectionIndicatorText.textContent = "Error";
connectionIndicator.title = connectionIndicator.title =
@ -199,7 +199,7 @@ async function testHostConnection() {
connectionStatus = false; connectionStatus = false;
}, },
corsissue: () => { corsissue: () => {
connectionIndicator.classList.add("cors-issue"); connectionIndicator.classList.add("webui-issue");
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"online", "online",
"offline", "offline",
@ -211,10 +211,23 @@ async function testHostConnection() {
"Server is online, but CORS is blocking our requests"; "Server is online, but CORS is blocking our requests";
connectionStatus = false; connectionStatus = false;
}, },
apiissue: () => {
connectionIndicator.classList.add("webui-issue");
connectionIndicator.classList.remove(
"online",
"offline",
"before",
"server-error"
);
connectionIndicatorText.textContent = "API";
connectionIndicator.title =
"Server is online, but the API seems to be disabled";
connectionStatus = false;
},
offline: () => { offline: () => {
connectionIndicator.classList.add("offline"); connectionIndicator.classList.add("offline");
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"cors-issue", "webui-issue",
"online", "online",
"before", "before",
"server-error" "server-error"
@ -227,7 +240,7 @@ async function testHostConnection() {
before: () => { before: () => {
connectionIndicator.classList.add("before"); connectionIndicator.classList.add("before");
connectionIndicator.classList.remove( connectionIndicator.classList.remove(
"cors-issue", "webui-issue",
"online", "online",
"offline", "offline",
"server-error" "server-error"
@ -254,12 +267,13 @@ async function testHostConnection() {
var url = document.getElementById("host").value + "/startup-events"; var url = document.getElementById("host").value + "/startup-events";
// Attempt normal request // Attempt normal request
try { try {
/** @type {Response} */ // Check if API is available
const response = await fetch(url, { const response = await fetch(
signal: AbortSignal.timeout(5000), document.getElementById("host").value + "/sdapi/v1/options",
}); {method: "OPTIONS"}
);
if (response.status === 200) { switch (response.status) {
case 200: {
setConnectionStatus("online"); setConnectionStatus("online");
// Load data as soon as connection is first stablished // Load data as soon as connection is first stablished
if (firstTimeOnline) { if (firstTimeOnline) {
@ -270,11 +284,21 @@ async function testHostConnection() {
getModels(); getModels();
firstTimeOnline = false; firstTimeOnline = false;
} }
} else { break;
setConnectionStatus("error"); }
const message = `Server responded with ${response.status} - ${response.statusText}. Try running the webui with the flag '--api'`; case 404: {
setConnectionStatus("apiissue");
const message = `The host is online, but the API seems to be disabled. Have you run the webui with the flag --api?`;
console.error(message); console.error(message);
if (notify) alert(message); if (notify) alert(message);
break;
}
default: {
setConnectionStatus("offline");
const message = `The host is online, but the API seems to be disabled. Have you run the webui with the flag --api?`;
console.error(message);
if (notify) alert(message);
}
} }
} catch (e) { } catch (e) {
try { try {