Skip to content

Commit

Permalink
Add GUI error handling per request
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Sep 6, 2024
1 parent 4c389cc commit 2dac9a3
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 84 deletions.
45 changes: 29 additions & 16 deletions src/tribler/ui/src/services/ipv8.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Circuit } from "@/models/circuit.model";
import { OverlayStats } from "@/models/overlay.model";
import axios, { AxiosInstance } from "axios";
import { handleHTTPError } from "./reporting";
import { handleHTTPError, handles } from "./reporting";


export class IPv8Service {
Expand All @@ -13,68 +13,81 @@ export class IPv8Service {
baseURL: this.baseURL,
withCredentials: true,
});
this.http.interceptors.response.use(function (response) { return response; }, handleHTTPError);
}


async enableDrift(enable: boolean): Promise<boolean> {
return (await this.http.put('/asyncio/drift', { enable })).data.success;
const response = await (this.http.put('/asyncio/drift', { enable: enable },
handles(200, 400)).catch(handleHTTPError));
if (response.status == 200)
return response.data.success;
return false;
}

async getDrift() {
return (await this.http.get('/asyncio/drift')).data.measurements;
const response = await (this.http.get('/asyncio/drift', handles(200, 404)).catch(handleHTTPError));
if (response.status == 200)
return response.data.measurements;
return [];
}

async getTasks() {
return (await this.http.get('/asyncio/tasks')).data.tasks;
return (await (this.http.get('/asyncio/tasks', handles(200)).catch(handleHTTPError))).data.tasks;
}

async setAsyncioDebug(enable: boolean, slownessThreshold: number): Promise<boolean> {
return (await this.http.put('/asyncio/debug', {enable: enable, slow_callback_duration: slownessThreshold})).data.success;
const response = await (this.http.put('/asyncio/debug',
{enable: enable, slow_callback_duration: slownessThreshold},
handles(200)).catch(handleHTTPError))
if (response.status == 200)
return response.data.success;
return false;
}

async getAsyncioDebug(): Promise<any> {
return (await this.http.get('/asyncio/debug')).data;
return (await (this.http.get('/asyncio/debug', handles(200)).catch(handleHTTPError))).data;
}

async getOverlays() {
return (await this.http.get('/overlays')).data.overlays;
return (await (this.http.get('/overlays', handles(200)).catch(handleHTTPError))).data.overlays;
}

async getOverlayStatistics(): Promise<OverlayStats[]> {
return (await this.http.get('/overlays/statistics')).data.statistics;
return (await (this.http.get('/overlays/statistics', handles(200)).catch(handleHTTPError))).data.statistics;
}

async getTunnelPeers() {
return (await this.http.get('/tunnel/peers')).data.peers;
return (await (this.http.get('/tunnel/peers', handles(200)).catch(handleHTTPError))).data.peers;
}

async getCircuits(): Promise<Circuit[]> {
return (await this.http.get('/tunnel/circuits')).data.circuits;
}

async getRelays() {
return (await this.http.get('/tunnel/relays')).data.relays;
return (await (this.http.get('/tunnel/relays', handles(200)).catch(handleHTTPError))).data.relays;
}

async getExits() {
return (await this.http.get('/tunnel/exits')).data.exits;
return (await (this.http.get('/tunnel/exits', handles(200)).catch(handleHTTPError))).data.exits;
}

async getSwarms() {
return (await this.http.get('/tunnel/swarms')).data.swarms;
return (await (this.http.get('/tunnel/swarms', handles(200)).catch(handleHTTPError))).data.swarms;
}

async getDHTStatistics() {
return (await this.http.get('/dht/statistics')).data.statistics;
return (await (this.http.get('/dht/statistics',
handles(200)).catch(handleHTTPError))).data.statistics; // Crash in case of 404
}

async getBuckets() {
return (await this.http.get('/dht/buckets')).data.buckets;
return (await (this.http.get('/dht/buckets', handles(200)).catch(handleHTTPError))).data.buckets;
}

async lookupDHTValue(hash: string) {
return this.http.get(`/dht/values/${hash}`);
return (await (this.http.get(`/dht/values/${hash}`,
handles(200)).catch(handleHTTPError))).data; // Crash in case of 404
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/tribler/ui/src/services/reporting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosError } from "axios";
import axios, { AxiosError, AxiosRequestConfig } from "axios";

export function handleHTTPError(error: Error | AxiosError) {
const error_popup_text = document.querySelector("#error_popup_text");
Expand All @@ -17,3 +17,7 @@ export function handleHTTPError(error: Error | AxiosError) {
}
return Promise.reject(error);
}

export function handles(...handled: number[]): AxiosRequestConfig {
return { validateStatus: function (status: number) { return handled.includes(status); } }
}
Loading

0 comments on commit 2dac9a3

Please sign in to comment.