improve response error handling,

add handler for network config,
fix import errors

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-05-15 22:41:54 +00:00
parent b94758c175
commit d6a7785ab0
5 changed files with 45 additions and 28 deletions

View File

@ -39,7 +39,7 @@
</div> </div>
</fieldset> </fieldset>
<fieldset class="w3-card w3-padding"> <fieldset class="w3-card w3-padding">
<span><legend>Network Interface</legend></span> <span><legend>Network Interfaces</legend></span>
<div class="input-grid" id="networks" style="grid-template-columns: auto auto auto 1fr;"></div> <div class="input-grid" id="networks" style="grid-template-columns: auto auto auto 1fr;"></div>
</fieldset> </fieldset>
<div class="w3-container w3-center" id="form-actions"> <div class="w3-container w3-center" id="form-actions">

View File

@ -452,16 +452,19 @@ function addNetworkLine (fieldset, netID, netDetails) {
icon.src = "images/resources/network.svg"; icon.src = "images/resources/network.svg";
icon.alt = netID; icon.alt = netID;
icon.dataset.network = netID; icon.dataset.network = netID;
icon.dataset.netvals = netDetails;
field.appendChild(icon); field.appendChild(icon);
let netLabel = document.createElement("label"); let netLabel = document.createElement("label");
netLabel.innerText = netID; netLabel.innerText = netID;
netLabel.dataset.network = netID; netLabel.dataset.network = netID;
netLabel.dataset.netvals = netDetails;
field.append(netLabel); field.append(netLabel);
let netDesc = document.createElement("p"); let netDesc = document.createElement("p");
netDesc.innerText = netDetails; netDesc.innerText = netDetails;
netDesc.dataset.network = netID; netDesc.dataset.network = netID;
netDesc.dataset.netvals = netDetails;
field.append(netDesc); field.append(netDesc);
let actionDiv = document.createElement("div"); let actionDiv = document.createElement("div");
@ -472,19 +475,41 @@ function addNetworkLine (fieldset, netID, netDetails) {
action.title = "Config Network"; action.title = "Config Network";
action.addEventListener("click", handleNetworkConfig); action.addEventListener("click", handleNetworkConfig);
action.dataset.network = netID; action.dataset.network = netID;
action.dataset.netvals = netDetails;
actionDiv.appendChild(action); actionDiv.appendChild(action);
field.append(actionDiv); field.append(actionDiv);
} }
async function handleNetworkConfig () { async function handleNetworkConfig () {
let netID = this.dataset.network; let netID = this.dataset.network;
let netDetails = this.dataset.netvals;
let header = `Edit ${netID}`; let header = `Edit ${netID}`;
let body = ``; let body = `<label for="rate">Rate Limit (MB/s)</label><input type="number" id="rate" name="rate" class="w3-input w3-border">`;
dialog(header, body, async (result, form) => { let d = dialog(header, body, async (result, form) => {
if (result === "confirm") { 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 () { async function handleFormExit () {
@ -499,9 +524,9 @@ async function handleFormExit () {
if (result.status === 200) { if (result.status === 200) {
await getConfig(); await getConfig();
populateDisk(); populateDisk();
goToPage("index.html");
} }
else { else {
alert(result.error); alert(result.error);
} }
goToPage("index.html");
} }

View File

@ -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 {alert, dialog} from "./dialog.js";
import {PVE} from "../vars.js"
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);

View File

@ -36,7 +36,7 @@ async function init (){
else { else {
alert("An error occured."); alert("An error occured.");
formSubmitButton.innerText = "LOGIN"; formSubmitButton.innerText = "LOGIN";
console.error(error); console.error(ticket.error);
} }
}); });
} }

View File

@ -1,12 +1,5 @@
import {API} from "/vars.js"; import {API} from "/vars.js";
export class NetworkError extends Error {
constructor(message) {
super(message);
this.name = "NetworkError";
}
}
export const resources_config = { export const resources_config = {
disk: { disk: {
actionBarOrder: ["move", "resize", "detach_attach", "delete"], actionBarOrder: ["move", "resize", "detach_attach", "delete"],
@ -144,25 +137,23 @@ export async function requestAPI (path, method, body = null) {
} }
async function request (url, content) { 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) { let response = await fetch(url, content);
return {status: 408, error: "network error"}; let data = null;
try {
data = await response.json();
data.status = response.status;
} }
else if(!response.ok){ catch {
let data = await response.json() data = null;
return {status: response.status, error: data.error}; }
if(!response.ok){
return {status: response.status, error: data ? data.error : response.status};
} }
else { else {
let data = await response.json();
data.status = response.status; data.status = response.status;
return data; return data ? data : response;
} }
} }