2023-04-07 16:02:47 +00:00
|
|
|
import { defineComponent, onErrorCaptured, ref } from 'vue'
|
2023-10-30 21:05:02 +00:00
|
|
|
import { useNuxtApp } from '../nuxt'
|
2022-03-16 15:49:53 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
2022-07-26 13:36:58 +00:00
|
|
|
emits: {
|
|
|
|
error (_error: unknown) {
|
|
|
|
return true
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2022-07-26 13:36:58 +00:00
|
|
|
},
|
2022-03-16 15:49:53 +00:00
|
|
|
setup (_props, { slots, emit }) {
|
2022-08-12 17:47:58 +00:00
|
|
|
const error = ref<Error | null>(null)
|
2022-03-16 15:49:53 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
|
2023-05-15 15:34:20 +00:00
|
|
|
onErrorCaptured((err, target, info) => {
|
2023-12-25 17:54:01 +00:00
|
|
|
if (import.meta.client && (!nuxtApp.isHydrating || !nuxtApp.payload.serverRendered)) {
|
2022-03-16 15:49:53 +00:00
|
|
|
emit('error', err)
|
2023-05-15 15:34:20 +00:00
|
|
|
nuxtApp.hooks.callHook('vue:error', err, target, info)
|
2022-03-16 15:49:53 +00:00
|
|
|
error.value = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-29 22:32:29 +00:00
|
|
|
function clearError () {
|
|
|
|
error.value = null
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => error.value ? slots.error?.({ error, clearError }) : slots.default?.()
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2022-03-16 15:49:53 +00:00
|
|
|
})
|