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>
</fieldset>
<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>
</fieldset>
<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.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 = ``;
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") {
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");
}

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

View File

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

View File

@ -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;
}
}