2022-07-21 14:29:03 +00:00
|
|
|
import { createError as _createError, H3Error } from 'h3'
|
2022-08-08 14:33:31 +00:00
|
|
|
import { toRef } from 'vue'
|
2022-09-14 09:22:03 +00:00
|
|
|
import { useNuxtApp } from '../nuxt'
|
2022-03-11 08:22:16 +00:00
|
|
|
|
2022-08-08 14:33:31 +00:00
|
|
|
export const useError = () => toRef(useNuxtApp().payload, 'error')
|
2022-03-11 08:22:16 +00:00
|
|
|
|
2022-07-21 14:29:03 +00:00
|
|
|
export interface NuxtError extends H3Error {}
|
|
|
|
|
|
|
|
export const showError = (_err: string | Error | Partial<NuxtError>) => {
|
|
|
|
const err = createError(_err)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
nuxtApp.callHook('app:error', err)
|
2022-08-08 14:33:31 +00:00
|
|
|
const error = useError()
|
|
|
|
error.value = error.value || err
|
2022-07-21 14:29:03 +00:00
|
|
|
} catch {
|
|
|
|
throw err
|
2022-03-11 08:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
export const clearError = async (options: { redirect?: string } = {}) => {
|
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
const error = useError()
|
|
|
|
nuxtApp.callHook('app:error:cleared', options)
|
|
|
|
if (options.redirect) {
|
|
|
|
await nuxtApp.$router.replace(options.redirect)
|
|
|
|
}
|
|
|
|
error.value = null
|
|
|
|
}
|
2022-07-21 14:29:03 +00:00
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
export const isNuxtError = (err?: string | object): err is NuxtError => !!(err && typeof err === 'object' && ('__nuxt_error' in err))
|
2022-07-21 14:29:03 +00:00
|
|
|
|
|
|
|
export const createError = (err: string | Partial<NuxtError>): NuxtError => {
|
|
|
|
const _err: NuxtError = _createError(err)
|
|
|
|
;(_err as any).__nuxt_error = true
|
|
|
|
return _err
|
|
|
|
}
|