From d6b576abcc0f794917aef4433d43445ae8280cf4 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 15 May 2023 22:41:54 +0000 Subject: [PATCH] improve response error handling, add handler for network config, fix import errors Signed-off-by: Arthur Lu --- config.html | 2 +- scripts/config.js | 33 +++++++++++++++++++++++++++++---- scripts/index.js | 3 ++- scripts/login.js | 2 +- scripts/utils.js | 33 ++++++++++++--------------------- 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/config.html b/config.html index b216a9f..1b5feed 100644 --- a/config.html +++ b/config.html @@ -39,7 +39,7 @@
- Network Interface + Network Interfaces
diff --git a/scripts/config.js b/scripts/config.js index 9665dbb..fca1600 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -452,16 +452,19 @@ function addNetworkLine (fieldset, netID, netDetails) { icon.src = "images/resources/network.svg"; icon.alt = netID; icon.dataset.network = netID; + icon.dataset.netvals = netDetails; field.appendChild(icon); let netLabel = document.createElement("label"); netLabel.innerText = netID; netLabel.dataset.network = netID; + netLabel.dataset.netvals = netDetails; field.append(netLabel); let netDesc = document.createElement("p"); netDesc.innerText = netDetails; netDesc.dataset.network = netID; + netDesc.dataset.netvals = netDetails; field.append(netDesc); let actionDiv = document.createElement("div"); @@ -472,19 +475,41 @@ function addNetworkLine (fieldset, netID, netDetails) { action.title = "Config Network"; action.addEventListener("click", handleNetworkConfig); action.dataset.network = netID; + action.dataset.netvals = netDetails; actionDiv.appendChild(action); field.append(actionDiv); } async function handleNetworkConfig () { let netID = this.dataset.network; + let netDetails = this.dataset.netvals; let header = `Edit ${netID}`; - let body = ``; - - dialog(header, body, async (result, form) => { + let body = ``; + + let d = dialog(header, body, async (result, form) => { if (result === "confirm") { + document.querySelector(`img[data-network="${netID}"]`).src = "images/status/loading.svg"; + let body = { + node: node, + type: type, + vmid: vmid, + netid: netID, + rate: form.get("rate") + } + let result = await requestAPI("/instance/network", "POST", body); + if (result.status === 200) { + await getConfig(); + populateNetworks(); + } + else{ + alert(result.error); + await getConfig(); + populateNetworks(); + } } }); + + d.querySelector("#rate").value = netDetails.split("rate=")[1].split(",")[0]; } async function handleFormExit () { @@ -499,9 +524,9 @@ async function handleFormExit () { if (result.status === 200) { await getConfig(); populateDisk(); + goToPage("index.html"); } else { alert(result.error); } - goToPage("index.html"); } \ No newline at end of file diff --git a/scripts/index.js b/scripts/index.js index 6c5f46b..da48f27 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -1,5 +1,6 @@ -import {requestPVE, requestAPI, goToPage, instances_config, nodes_config} from "./utils.js"; +import {requestPVE, requestAPI, goToPage, goToURL, instances_config, nodes_config} from "./utils.js"; import {alert, dialog} from "./dialog.js"; +import {PVE} from "../vars.js" window.addEventListener("DOMContentLoaded", init); diff --git a/scripts/login.js b/scripts/login.js index c86a251..e6ec7b9 100644 --- a/scripts/login.js +++ b/scripts/login.js @@ -36,7 +36,7 @@ async function init (){ else { alert("An error occured."); formSubmitButton.innerText = "LOGIN"; - console.error(error); + console.error(ticket.error); } }); } \ No newline at end of file diff --git a/scripts/utils.js b/scripts/utils.js index 687af55..5aba13a 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,12 +1,5 @@ import {API} from "/vars.js"; -export class NetworkError extends Error { - constructor(message) { - super(message); - this.name = "NetworkError"; - } -} - export const resources_config = { disk: { actionBarOrder: ["move", "resize", "detach_attach", "delete"], @@ -144,25 +137,23 @@ export async function requestAPI (path, method, body = null) { } async function request (url, content) { - let response = await fetch(url, content) - .then((response) => { - return response; - }) - .catch((error) => { - return new NetworkError(error); - }); - if (response instanceof NetworkError) { - return {status: 408, error: "network error"}; + let response = await fetch(url, content); + let data = null; + try { + data = await response.json(); + data.status = response.status; } - else if(!response.ok){ - let data = await response.json() - return {status: response.status, error: data.error}; + catch { + data = null; + } + + if(!response.ok){ + return {status: response.status, error: data ? data.error : response.status}; } else { - let data = await response.json(); data.status = response.status; - return data; + return data ? data : response; } }