Skip to content

Commit

Permalink
web: fix error handling (#800)
Browse files Browse the repository at this point in the history
Correctly handle errors that occur in the worker.
  • Loading branch information
wydengyre authored Oct 13, 2024
1 parent dc2a910 commit eaf5e01
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
77 changes: 51 additions & 26 deletions packages/web/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,45 +118,70 @@ async function onSubmit(event: Event) {
window.location.href = URL.createObjectURL(blob);
}

window.onerror = (message, source, lineno, colno, error) => {
let outErr = error;
if (outErr === undefined) {
const outMessage = message.toString();
outErr = new Error(outMessage);
}
handleError(
outErr,
"window.onerror",
String(message),
String(source),
lineno ?? null,
colno ?? null,
String(error),
);
};

window.onunhandledrejection = (event) => {
const reason = String(event.reason);
const err = new Error(reason);
handleError(err, "window.onunhandledrejection", reason);
};

// Improved model error handling
function handleError(
error: Error,
type: string,
...details: (string | number | null)[]
) {
model.loading = false;
model.error = error;
updateUI();

console.error(type, ...details);

// Prepare body for sending
const body = JSON.stringify([type, clientId, version, ...details]);

if (navigator.sendBeacon) {
navigator.sendBeacon("/error", body);
} else {
fetch("/error", {
method: "POST",
headers: { "Content-Type": "application/json" },
body,
}).then(() => {}); // fire-and-forget
}
}

function updateUI() {
const { sourceFile, targetFile, loading } = model;
const derivedState = {
error: model.error?.message ?? "",
submissionEnabled: sourceFile !== null && targetFile !== null && !loading,
submitButtonText: loading ? "Loading" : "View HTML",
};
console.log("updateUI", derivedState);
console.log("model", model);

errDiv.innerText = derivedState.error;
submitButton.disabled = !derivedState.submissionEnabled;
submitButton.textContent = derivedState.submitButtonText;
}

window.onerror = (message, source, lineno, colno, error) => {
console.error("window.onerror", message, source, lineno, colno, error);
if (error === undefined) {
return;
}
model.loading = false;
model.error = error;
updateUI();

// purposely fire-and-forget
const body = JSON.stringify([
clientId,
version,
message,
source,
lineno,
colno,
error,
]);
fetch("/error", {
method: "POST",
headers: { "Content-Type": "application/json" },
body,
});
};

if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", loadDom);
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function renderAlignment(
epubToText(sourceData),
epubToText(targetData),
]);
console.log("got source text", sourceText.length);
const sourceLang = franc(sourceText);
const targetLang = franc(targetText);

Expand Down

0 comments on commit eaf5e01

Please sign in to comment.