Skip to content

Commit

Permalink
frontend: create a error file containing a backtrace and download it …
Browse files Browse the repository at this point in the history
…when an unhandled exception is thrown in the app.
  • Loading branch information
ffreddow committed Dec 12, 2024
1 parent 5352890 commit 8d69708
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
16 changes: 9 additions & 7 deletions frontend/src/components/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Worker from '../worker?worker'
import { Row, Spinner } from 'react-bootstrap';
import { connected, connected_to, secret } from './charger_list';
import { setAppNavigation } from './Navbar';
import { enableLogging, refreshPromise } from '../utils';
import { enableLogging, refresh_access_token } from '../utils';
import Median from "median-js-bridge";
import i18n from '../i18n';
import { showAlert } from './Alert';
Expand Down Expand Up @@ -89,12 +89,6 @@ export class Frame extends Component {
handleErrorMessage(msg: Message) {
connected.value = false;
connected_to.value = "";
if (msg.data.translation) {
const error = msg.data as ErrorMessage;
showAlert(i18n.t(error.translation, error.format) as string, "danger");
} else {
showAlert(msg.data, "danger");
}
}

startWorker() {
Expand All @@ -110,6 +104,14 @@ export class Frame extends Component {
this.worker.terminate();
break;
}
} else if (e.data.unresolved) {
const msg = JSON.stringify(e.data.msg);
const blob = new Blob([msg]);
const url = URL.createObjectURL(blob);
const filename = `mystaging_error_${Date.now()}.json`;
if (Median.isNativeApp()) {
Median.share.downloadFile({url: url, filename: filename, open: true});
}
} else {
const msg = e.data as Message;
switch (msg.type) {
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ setInterval(async () => {
await refresh_access_token();
}, 1000 * 60 * 5);

addEventListener("unhandledrejection", (event) => {
const stack = event.reason.stack.split("\n");

const evt = {
message: event.reason.message,
stack: stack
}
const msg = JSON.stringify(evt);
const blob = new Blob([msg]);
const url = URL.createObjectURL(blob);
const filename = `mystaging_error_${Date.now()}.json`
if (Median.isNativeApp()) {
Median.share.downloadFile({url: url, filename: filename, open: true});
}
});

export function App() {
const {t} = useTranslation("", {useSuspense: false});
Expand Down
18 changes: 12 additions & 6 deletions frontend/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ const tunnel_url = import.meta.env.VITE_BACKEND_WS_URL + "/ws?key_id="
let wgClient: Client | undefined = undefined;
let setup_data: SetupMessage;

self.addEventListener("unhandledrejection", (event) => {
const stack = event.reason.stack.split("\n");

const evt = {
message: event.reason.message,
stack: stack
}
self.postMessage({unresolved: true, msg: evt});
});

self.addEventListener("message", async (e: MessageEvent) => {
if (typeof e.data === "string") {
switch (e.data) {
Expand Down Expand Up @@ -122,8 +132,9 @@ function disconnect_cb() {

async function start_connection(setup_data: SetupMessage) {
let keys: any;
const url = import.meta.env.VITE_BACKEND_URL + "/charger/get_key?cid=" + setup_data.chargerID;
try {
const resp = await fetch(import.meta.env.VITE_BACKEND_URL + "/charger/get_key?cid=" + setup_data.chargerID, {credentials: "same-origin"});
const resp = await fetch(url, {credentials: "same-origin"});
if (resp.status === 404) {
const msg: Message = {
type: MessageType.Error,
Expand All @@ -150,11 +161,6 @@ async function start_connection(setup_data: SetupMessage) {
}
keys = await resp.json();
} catch (e) {
const msg: Message = {
type: MessageType.Error,
data: `Getting connection key crashed: ${e}`,
}
self.postMessage(msg);
return;
}
const decrypted_keys = decrypt_keys(keys, setup_data.secret);
Expand Down

0 comments on commit 8d69708

Please sign in to comment.