Skip to content

Commit fa15146

Browse files
committed
feat: improve global errors handling
1 parent 8c26b47 commit fa15146

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ window[ApplicationNamespace].functions.changeGlobalStates = changeGlobalState;
293293
<!-- Global error boundary -->
294294
<ErrorBoundary>
295295
<template #default>
296-
<Layout v-if="!globalStates.customLayout">
296+
<Layout v-if="!globalStates.customLayout" :page="globalStates.page">
297297
<Router
298298
v-if="globalStates.page !== 'none'"
299299
:page="globalStates.page"

src/components/handlers/ErrorBoundary.vue

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<script setup lang="ts">
2-
import { computed, onErrorCaptured, ref } from "vue";
2+
import { computed, onErrorCaptured, ref, watchEffect } from "vue";
33
import { log } from "@/lib/handlers/log.ts";
44
import { extractError } from "@/lib/helpers/extract-error.ts";
55
6+
const { resetKey } = defineProps<{
7+
"resetKey"?: string;
8+
}>();
69
const currentError = ref<{ "name": string; "message": string; "stack": string } | undefined>(undefined);
710
811
// Listen for errors
912
onErrorCaptured((error: Error) => {
10-
log.error("A global error was captured:", JSON.stringify(error));
11-
currentError.value = extractError(error);
13+
const extractedError = extractError(error);
14+
15+
log.error(
16+
"A global error was captured:",
17+
extractedError.name + ":",
18+
extractedError.message + ";",
19+
"Stack:",
20+
extractedError.stack,
21+
);
22+
currentError.value = extractedError;
1223
1324
// Prevent error from bubbling further
1425
return false;
@@ -24,6 +35,12 @@ const slotProperties = computed(() => {
2435
});
2536
// Show the 'error' template if there is an error
2637
const slotName = computed(() => (currentError.value ? "error" : "default"));
38+
39+
watchEffect(() => {
40+
if (resetKey) {
41+
currentError.value = undefined;
42+
}
43+
});
2744
</script>
2845

2946
<template>

src/components/layout/Layout.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { ref } from "vue";
55
import ContextMenu from "@/components/layout/ContextMenu.vue";
66
import { useEventListener } from "@vueuse/core";
77
import { ApplicationNamespace } from "@/constants/application.ts";
8+
import type { RouteType } from "@/types/application/route.type.ts";
89
10+
const { page } = defineProps<{
11+
"page": RouteType;
12+
}>();
913
const contextMenu = ref<{
1014
"opened": boolean;
1115
"x" : number;
@@ -70,7 +74,7 @@ useEventListener(window, "mousedown", (event: MouseEvent) => {
7074
/>
7175
<Sidebar />
7276
<!-- Pages error boundary -->
73-
<ErrorBoundary>
77+
<ErrorBoundary :resetKey="page">
7478
<template #default>
7579
<slot />
7680
</template>

src/lib/launcher/core/download.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,4 @@ async function downloadLibraries(libraries: Library[], version: string) {
148148
);
149149
}
150150
}
151-
}
151+
}

src/pages/Home.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
<script setup lang="ts">
22
3-
import { inject, onMounted } from "vue";
3+
import { inject } from "vue";
44
import type { GlobalStatesChangerType } from "@/types/application/global-states.type.ts";
55
import { GlobalStatesChangerContextKey } from "@/constants/application.ts";
66
77
const changeGlobalState = inject<GlobalStatesChangerType>(GlobalStatesChangerContextKey);
88
99
async function download() { /* TODO */ }
1010
async function run() { /* TODO */ }
11-
12-
onMounted(() => {
13-
throw new Error("myheavyass 🥀");
14-
});
1511
</script>
1612

1713
<template>

0 commit comments

Comments
 (0)