Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
(vc/dev2) Attempt to fix CrashHandler odd issues
Browse files Browse the repository at this point in the history
  • Loading branch information
verticalsync committed Feb 14, 2024
1 parent 38a2ad4 commit 6d509a6
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/plugins/crashHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const settings = definePluginSettings({
});

let hasCrashedOnce = false;
let isRecovering = false;
let shouldAttemptRecover = true;

export default definePlugin({
Expand All @@ -71,38 +72,49 @@ export default definePlugin({
{
find: ".Messages.ERRORS_UNEXPECTED_CRASH",
replacement: {
match: /(?=this\.setState\()/,
replace: "$self.handleCrash(this);"
match: /this\.setState\((.+?)\)/,
replace: "$self.handleCrash(this,$1);"
}
}
],

handleCrash(_this: any) {
handleCrash(_this: any, errorState: any) {
_this.setState(errorState);

// Already recovering, prevent error which happens more than once too fast to trigger another recover
if (isRecovering) return;
isRecovering = true;

// 1 ms timeout to avoid react breaking when re-rendering
setTimeout(() => {
if (!shouldAttemptRecover) {
try {
showNotification({
color: "#eed202",
title: "Discord has crashed!",
body: "Awn :( Discord has crashed two times rapidly, not attempting to recover.",
noPersist: true,
});
} catch { }

return;
}
try {
// Prevent a crash loop with an error that could not be handled
if (!shouldAttemptRecover) {
try {
showNotification({
color: "#eed202",
title: "Discord has crashed!",
body: "Awn :( Discord has crashed two times rapidly, not attempting to recover.",
noPersist: true
});
} catch { }

return;
}

shouldAttemptRecover = false;
// This is enough to avoid a crash loop
setTimeout(() => shouldAttemptRecover = true, 500);
shouldAttemptRecover = false;
// This is enough to avoid a crash loop
setTimeout(() => shouldAttemptRecover = true, 500);
} catch { }

try {
if (!hasCrashedOnce) {
hasCrashedOnce = true;
maybePromptToUpdate("Uh oh, Discord has just crashed... but good news, there is a Vencord update available that might fix this issue! Would you like to update now?", true);
}
} catch { }

try {
if (settings.store.attemptToPreventCrashes) {
this.handlePreventCrash(_this);
}
Expand All @@ -118,7 +130,7 @@ export default definePlugin({
color: "#eed202",
title: "Discord has crashed!",
body: "Attempting to recover...",
noPersist: true,
noPersist: true
});
} catch { }

Expand Down Expand Up @@ -169,6 +181,10 @@ export default definePlugin({
}
}


// Set isRecovering to false before setting the state to allow us to handle the next crash error correcty, in case it happens
setImmediate(() => isRecovering = false);

try {
_this.setState({ error: null, info: null });
} catch (err) {
Expand Down

0 comments on commit 6d509a6

Please sign in to comment.