Skip to content

Commit 451b183

Browse files
JohnJohn
authored andcommitted
#255: Error dialogs
1 parent 584a832 commit 451b183

File tree

8 files changed

+43
-30
lines changed

8 files changed

+43
-30
lines changed

server/main.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ const app: Express = express()
3232
app.use(express.static('renderer'))
3333
app.use(cors(options))
3434
app.use(express.json());
35-
app.use((err: Error, req: Request, res: Response, next: NextFunction): void => {
36-
res.status(500);
37-
res.render('error', { error: err })
38-
});
3935

4036
/**
4137
* Execute a plugin module function via API call
@@ -51,7 +47,7 @@ app.post('/api/v1/invokeFunction', (req: Request, res: Response, next: NextFunct
5147
const args = req.body["args"];
5248
switch (method) {
5349
case "deleteFile":
54-
deleteFile(args).then(() => res.json(Object())).catch((err: any) => next());
50+
deleteFile(args).then(() => res.json(Object())).catch((err: any) => next(err));
5551
break;
5652
case "downloadFile":
5753
downloadFile(args.downloadUrl, args.fileName).then(() => res.json(Object())).catch((err: any) => next(err));
@@ -83,6 +79,11 @@ app.post('/api/v1/downloadProgress', (req: Request, res: Response): void => {
8379
res.json(Object());
8480
});
8581

82+
app.use((err: Error, req: Request, res: Response, next: NextFunction): void => {
83+
res.status(500);
84+
res.json({ error: err?.message ?? "Internal Server Error" })
85+
});
86+
8687
app.listen(port, () => console.log(`Application is running on port ${port}`));
8788

8889

@@ -113,7 +114,7 @@ function downloadModel(downloadUrl: string, fileName: string): void {
113114
fileName,
114115
success: undefined
115116
};
116-
console.log("downloading file", fileName, state.percent);
117+
console.log("downloading file", fileName, (state.percent * 100).toFixed(2) + '%');
117118
})
118119
.on("error", function (err: Error) {
119120
downloadProgress[fileName] = {
@@ -158,11 +159,14 @@ function downloadingFile(): ProgressState | undefined {
158159
async function downloadFile(downloadUrl: string, fileName: string): Promise<void> {
159160
return new Promise((resolve, reject) => {
160161
const obj = downloadingFile();
161-
if (obj) reject(Error(obj.fileName + " is being downloaded!"));
162+
if (obj) {
163+
reject(Error(obj.fileName + " is being downloaded!"))
164+
return;
165+
};
162166
(async () => {
163167
downloadModel(downloadUrl, fileName);
164168
})().catch(e => {
165-
console.log("downloadModel", fileName, e);
169+
console.error("downloadModel", fileName, e);
166170
});
167171
resolve();
168172
});

web/app/_helpers/EventHandler.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ export default function EventHandler({ children }: { children: ReactNode }) {
2626
}
2727

2828
function handleDownloadUpdate(state: any) {
29-
console.log("handleDownloadUpdate", state);
3029
if (!state) return;
3130
setDownloadState(state);
3231
}
3332

3433
function handleDownloadSuccess(state: any) {
35-
console.log("handleDownloadSuccess", state);
3634
if (state && state.fileName && state.success === true) {
3735
setDownloadStateSuccess(state.fileName);
3836
executeSerial(ModelManagementService.UpdateFinishedDownloadAt, state.fileName).then(() => {

web/app/_services/coreService.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { store } from "./storeService";
22
import { EventEmitter } from "./eventsService";
33
import { version } from '../../package.json';
4+
import { toast } from 'react-toastify';
45
const API_BASE_PATH: string = "/api/v1";
56
export const setupCoreServices = () => {
67
if (typeof window === "undefined") {
@@ -29,15 +30,15 @@ async function appVersion() {
2930
}
3031

3132
function invokePluginFunc(modulePath: string, pluginFunc: string, ...args: any): Promise<any> {
32-
return fetchApi(modulePath, pluginFunc, args);
33+
return fetchApi(modulePath, pluginFunc, args).catch((err: Error) => { throw err });
3334
};
3435

3536
async function downloadFile(downloadUrl: string, fileName: string) {
36-
return fetchApi("", "downloadFile", { downloadUrl: downloadUrl, fileName: fileName });
37+
return fetchApi("", "downloadFile", { downloadUrl: downloadUrl, fileName: fileName }).catch((err: Error) => { throw err });
3738
}
3839

3940
async function deleteFile(fileName: string) {
40-
return fetchApi("", "deleteFile", fileName);
41+
return fetchApi("", "deleteFile", fileName).catch((err: Error) => { throw err });
4142
}
4243

4344
async function fetchApi(modulePath: string, pluginFunc: string, args: any): Promise<any> {
@@ -48,21 +49,27 @@ async function fetchApi(modulePath: string, pluginFunc: string, args: any): Prom
4849
});
4950

5051
if (!response.ok) {
51-
console.error("Error");
52-
return null;
53-
}
54-
else if (response.status >= 400) {
55-
console.error('HTTP Error: ' + response.status + ' - ' + response.text);
52+
const json = await response.json();
53+
if (json && json.error) {
54+
toast.error(json.error, {
55+
position: "bottom-left",
56+
autoClose: 5000,
57+
hideProgressBar: false,
58+
closeOnClick: true,
59+
pauseOnHover: true,
60+
draggable: true,
61+
progress: undefined,
62+
theme: "light",
63+
});
64+
}
5665
return null;
5766
}
58-
else {
59-
const text = await response.text();
60-
try {
61-
const json = JSON.parse(text)
62-
return Promise.resolve(json);
63-
} catch (err) {
64-
return Promise.resolve(text);
65-
}
67+
const text = await response.text();
68+
try {
69+
const json = JSON.parse(text)
70+
return Promise.resolve(json);
71+
} catch (err) {
72+
return Promise.resolve(text);
6673
}
6774
}
6875

web/app/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import EventListenerWrapper from "./_helpers/EventListenerWrapper";
1111
import { setupCoreServices } from "./_services/coreService";
1212
import MainContainer from "./_components/MainContainer";
1313
import { executeSerial } from "../../electron/core/plugin-manager/execution/extension-manager";
14+
import { ToastContainer } from 'react-toastify';
15+
import 'react-toastify/dist/ReactToastify.css';
1416

1517
const Page: React.FC = () => {
1618
const [setupCore, setSetupCore] = useState(false);
@@ -76,6 +78,9 @@ const Page: React.FC = () => {
7678
</ThemeWrapper>
7779
</EventListenerWrapper>
7880
)}
81+
<div>
82+
<ToastContainer />
83+
</div>
7984
</JotaiWrapper>
8085
);
8186
};

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"react": "18.2.0",
3939
"react-dom": "18.2.0",
4040
"react-hook-form": "^7.45.4",
41+
"react-toastify": "^9.1.3",
4142
"tailwindcss": "3.3.3",
4243
"typescript": "5.1.6"
4344
},

web/public/plugins/inference-plugin/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/public/plugins/model-management-plugin/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ function fetchDownloadProgress(fileName, intervalId) {
574574
return;
575575
}
576576
const json = yield response.json();
577-
console.log("fetchDownloadProgress", json);
578577
if (isEmptyObject(json)) {
579578
if (!fileName) {
580579
clearInterval(intervalId);
@@ -587,7 +586,6 @@ function fetchDownloadProgress(fileName, intervalId) {
587586
}
588587
else {
589588
events.emit(EventName.OnDownloadUpdate, json);
590-
console.log("OnDownloadUpdate", json);
591589
}
592590
});
593591
}

web/public/plugins/monitoring-plugin/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)