feat: Add global error handler (#1834)

This commit is contained in:
Rémi Marseault 2024-08-18 14:40:56 -05:00 committed by GitHub
parent fc2fc7adc3
commit b241617e6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View file

@ -14,6 +14,8 @@
<meta name="theme-color" content="#000" />
<title>VueTorrent</title>
<script type="module" src="/src/globalErrorHandler.ts"></script>
<!-- PWA setup -->
<link rel="manifest" href="manifest.webmanifest" crossorigin="use-credentials" />
<script src="registerSW.js"></script>

View file

@ -8,7 +8,7 @@ import { formatPercent, formatSpeed } from '@/helpers'
import { backend } from '@/services/backend'
import { useAddTorrentStore, useAppStore, useDialogStore, useLogStore, useMaindataStore, usePreferenceStore, useTorrentStore, useVueTorrentStore } from '@/stores'
import { storeToRefs } from 'pinia'
import { onBeforeMount, watch, watchEffect } from 'vue'
import { onBeforeMount, onMounted, watch, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'
import { toast } from 'vue3-toastify'
@ -67,6 +67,11 @@ onBeforeMount(() => {
addLaunchQueueConsumer()
})
onMounted(() => {
// Global error handler flag
sessionStorage.setItem('vuetorrent_mounted', 'true')
})
watch(
() => appStore.isAuthenticated,
async isAuthenticated => {

15
src/globalErrorHandler.ts Normal file
View file

@ -0,0 +1,15 @@
window.onerror = function (msg, url, line, col, error) {
let extra = ''
// only add 'col' and 'error' if they are available
col && (extra += '\ncolumn: ' + col)
error && (extra += '\nerror: ' + error)
const final_message = 'Error: ' + msg + '\nurl: ' + url + '\nline: ' + line + extra
if (sessionStorage.getItem('vuetorrent_mounted') === 'true') {
console.error(final_message)
} else {
alert(final_message)
}
}