mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-28 18:46:02 +00:00
api error handling
This commit is contained in:
@@ -31,9 +31,14 @@ module.exports = function useController(app, electron, route, controller) {
|
|||||||
const handler = `${route.substring(1)}-${_.kebabCase(key)}`;
|
const handler = `${route.substring(1)}-${_.kebabCase(key)}`;
|
||||||
// console.log('REGISTERING HANDLER', handler);
|
// console.log('REGISTERING HANDLER', handler);
|
||||||
electron.ipcMain.handle(handler, async (event, args) => {
|
electron.ipcMain.handle(handler, async (event, args) => {
|
||||||
|
try {
|
||||||
const data = await controller[key](args);
|
const data = await controller[key](args);
|
||||||
// console.log('HANDLED API', handler, data);
|
// console.log('HANDLED API', handler, data);
|
||||||
|
if (data === undefined) return null;
|
||||||
return data;
|
return data;
|
||||||
|
} catch (err) {
|
||||||
|
return { apiErrorMessage: err.message };
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +73,7 @@ module.exports = function useController(app, electron, route, controller) {
|
|||||||
res.json(data);
|
res.json(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
res.status(500).json({ error: e.message });
|
res.status(500).json({ apiErrorMessage: e.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { writable } from 'svelte/store';
|
|||||||
// import { cacheClean } from './cache';
|
// import { cacheClean } from './cache';
|
||||||
import getElectron from './getElectron';
|
import getElectron from './getElectron';
|
||||||
// import socket from './socket';
|
// import socket from './socket';
|
||||||
|
import { showSnackbarError } from '../utility/snackbar';
|
||||||
|
|
||||||
let eventSource;
|
let eventSource;
|
||||||
let apiLogging = false;
|
let apiLogging = false;
|
||||||
@@ -15,6 +16,16 @@ function wantEventSource() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function processApiResponse(route, args, resp) {
|
||||||
|
if (apiLogging) {
|
||||||
|
console.log('<<< API RESPONSE', route, args, resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resp?.apiErrorMessage) {
|
||||||
|
showSnackbarError(resp?.apiErrorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function apiCall(route: string, args: {} = undefined) {
|
export async function apiCall(route: string, args: {} = undefined) {
|
||||||
if (apiLogging) {
|
if (apiLogging) {
|
||||||
console.log('>>> API CALL', route, args);
|
console.log('>>> API CALL', route, args);
|
||||||
@@ -23,11 +34,7 @@ export async function apiCall(route: string, args: {} = undefined) {
|
|||||||
const electron = getElectron();
|
const electron = getElectron();
|
||||||
if (electron) {
|
if (electron) {
|
||||||
const resp = await electron.invoke(route.replace('/', '-'), args);
|
const resp = await electron.invoke(route.replace('/', '-'), args);
|
||||||
|
processApiResponse(route, args, resp);
|
||||||
if (apiLogging) {
|
|
||||||
console.log('<<< API RESPONSE', route, args, resp);
|
|
||||||
}
|
|
||||||
|
|
||||||
return resp;
|
return resp;
|
||||||
} else {
|
} else {
|
||||||
const resp = await fetch(`${resolveApi()}/${route}`, {
|
const resp = await fetch(`${resolveApi()}/${route}`, {
|
||||||
@@ -41,11 +48,7 @@ export async function apiCall(route: string, args: {} = undefined) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const json = await resp.json();
|
const json = await resp.json();
|
||||||
|
processApiResponse(route, args, json);
|
||||||
if (apiLogging) {
|
|
||||||
console.log('<<< API RESPONSE', route, args, json);
|
|
||||||
}
|
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user