Attempt to fix CrashHandler odd issues

main
Nuckyz 9 months ago
parent 0c9d2a6a21
commit 48822bdc58
No known key found for this signature in database
GPG Key ID: 440BF8296E1C4AD9

@ -57,6 +57,7 @@ const settings = definePluginSettings({
}); });
let hasCrashedOnce = false; let hasCrashedOnce = false;
let isRecovering = false;
let shouldAttemptRecover = true; let shouldAttemptRecover = true;
export default definePlugin({ export default definePlugin({
@ -71,38 +72,49 @@ export default definePlugin({
{ {
find: ".Messages.ERRORS_UNEXPECTED_CRASH", find: ".Messages.ERRORS_UNEXPECTED_CRASH",
replacement: { replacement: {
match: /(?=this\.setState\()/, match: /this\.setState\((.+?)\)/,
replace: "$self.handleCrash(this);" 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 // 1 ms timeout to avoid react breaking when re-rendering
setTimeout(() => { setTimeout(() => {
if (!shouldAttemptRecover) { try {
try { // Prevent a crash loop with an error that could not be handled
showNotification({ if (!shouldAttemptRecover) {
color: "#eed202", try {
title: "Discord has crashed!", showNotification({
body: "Awn :( Discord has crashed two times rapidly, not attempting to recover.", color: "#eed202",
noPersist: true, title: "Discord has crashed!",
}); body: "Awn :( Discord has crashed two times rapidly, not attempting to recover.",
} catch { } noPersist: true
});
return; } catch { }
}
return;
}
shouldAttemptRecover = false; shouldAttemptRecover = false;
// This is enough to avoid a crash loop // This is enough to avoid a crash loop
setTimeout(() => shouldAttemptRecover = true, 500); setTimeout(() => shouldAttemptRecover = true, 500);
} catch { }
try { try {
if (!hasCrashedOnce) { if (!hasCrashedOnce) {
hasCrashedOnce = true; 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); 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) { if (settings.store.attemptToPreventCrashes) {
this.handlePreventCrash(_this); this.handlePreventCrash(_this);
} }
@ -118,7 +130,7 @@ export default definePlugin({
color: "#eed202", color: "#eed202",
title: "Discord has crashed!", title: "Discord has crashed!",
body: "Attempting to recover...", body: "Attempting to recover...",
noPersist: true, noPersist: true
}); });
} catch { } } catch { }
@ -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 { try {
_this.setState({ error: null, info: null }); _this.setState({ error: null, info: null });
} catch (err) { } catch (err) {

Loading…
Cancel
Save