NuxtN
Nuxt14mo ago
Yutako

"useAsyncData" does not show error page on "createError"

Hey there, i want to create a composable for my api. Which just is a wrapper for the useAsyncData component:

Within that wrapper i want to throw an global error, in case the api response with a status code >= 500

export default (handler, options) {
   // $client is just an instance of $fetch.create()
   const {$client} = useApiClient()

    return useAsyncData<TRes>(async () => {
        try {
            return await handler($client)
        } catch (e) {
            // global error on api 5xx errors
            if ((e instanceof FetchError || e instanceof H3Error) && e.statusCode >= 500) {
                console.log('This line is executed. Condition is true')
                throw createError({
                  fatal: true,
                  status: 500,
                  message: 'Foo'
                })
            }

            throw e
        }
    }, options)
}


And calling that in my page component:

<script setup lang="ts">
const {data} = await useApiData(($client) => $client('/page/will/be/5xx')))
</script>


This console.log line is shown in the cli (ssr and csr) but the error-page is never shown, not when refreshing the page or navigating using the vue-router.
Was this page helpful?