I'm working on a Nuxt 3 application, and I'm using the useFetch function from nuxt3 to make API requests. I want to handle errors returned by useFetch and pass them to the Nuxt 3 error.page for display.
Here's my current code setup:
In api.ts:
import { useRuntimeConfig, useFetch } from 'nuxt3';
export const api = async (request: string, opts: any) => {
const config = useRuntimeConfig();
try {
const fetch = await useFetch(request, {
baseURL: config.public.API_BASE_URL,
...opts,
headers: {
Authorization: `Bearer ${getToken()}`,
},
});
//i tried this one and also CreateError a mentioned into the docs.
if (fetch.error?.value) {
throw new Error(
`${fetch.error.value.statusCode} ${fetch.error.value.statusMessage}`
);
}
return fetch;
} catch (error) {
throw error;
}
};
and In error.vue:
<template>
<NuxtLayout>
<CommonLayoutUtilsErrorLayout
:status-code="error.statusCode"
:error-message="error.message"
></CommonLayoutUtilsErrorLayout>
</NuxtLayout>
</template>
<script>
export default {
props: {
error: {
type: Object,
default: null,
},
errorMessage: String,
},
useHead() {
return {
title: `${this.error.statusCode} Error`,
};
},
};
</script>
I want to know how to properly pass errors from the useFetch function to the error.page in Nuxt 3 so that I can display the error message and status code on the error page. Any guidance or examples would be greatly appreciated. Thank you!
I tried this one
if (fetch.error?.value) {
throw new Error(
`${fetch.error.value.statusCode} ${fetch.error.value.statusMessage}`
);
}
and this one too
if (fetch.error?.value) {
// throw createError({ statusCode: fetch.error?.value?.statusCode, message: fetch.error?.value?.statusMessage })
throw createError({ statusCode: 404, statusMessage: 'Page Not Found', fatal: true })
}
The
useFetchcomposable has anerrorobject available as a return value. See exampleA simple example would be something like this.
On the client side, by default, it will throw a non-fatal error for you to handle. If you need to trigger a full-screen error page, then you can do this by setting
fatal: true(See my example).error.vueIf you also want to clear the error. Check this doc
Hope that helps.