Custom useFetch and error handling
Hi guys,
I'm using Nuxt 3.16.1 in SSR.
I'm tryin to create a custom useFetch to factorize my API calls, but what I want is : when there's an error during the server rendering, before hydration, throw the error returned by the API using my error.vue file.
Here are my custom useFetch files :
7 Replies
Hi @Quentin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In the first example, I see the status code in my SSR console, but the error is not thrown.
In the second example, the error is thrown and I see the error page.
But in this case, I'll have to check the error.value each time i'm using useApi().fetch().
And i've near a hundred of those so it's not ideal.
----------------------
Your approach to creating a custom useFetch composable and handling errors in Nuxt 3 is on the right track, but there are a few important details and best practices to consider for SSR error handling and integration with Nuxt's error system.
Key Points from Nuxt Docs
1. Throwing Errors During SSR
To trigger Nuxt's error page (error.vue) during SSR, you should throw an error using createError inside your composable or page. This will cause Nuxt to render the error page before hydration, as you want. The recommended way is to use createError with appropriate statusCode and statusMessage. This is shown in the official docs: Error Handling: createError 2. Custom useFetch Composable
When creating a custom useFetch (like your useApi), you should pass your custom $fetch instance via the $fetch option. This is the correct pattern and is documented in the Nuxt recipes: Custom useFetch Recipe 3. Error Handling in onResponse
Throwing createError inside onResponse is valid, but you should ensure that the error is thrown in a way that Nuxt can catch it during SSR. Your current implementation is close, but you might want to set a more descriptive statusMessage and ensure you are not leaking sensitive data. 4. TypeScript and Error Types
If you want to type your errors, you can use the FetchError type from ofetch, as shown in the docs: Custom useFetch Error Typing Recommendations - Throw createError in onResponse for SSR:
Your pattern is correct. Throwing createError in onResponse will trigger Nuxt's error page during SSR. - Set statusMessage appropriately:
Use response.statusText or a custom message, not the request object. - Use $fetch in useFetch:
Pass your custom $fetch instance via the $fetch option, as you are doing. - Avoid leaking sensitive data:
Do not include the full request object in the error message. Example Adjustment This will ensure that during SSR, if your API returns an error, Nuxt will render error.vue as expected. For more details and examples, see the official Custom useFetch Recipe and Error Handling documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
To trigger Nuxt's error page (error.vue) during SSR, you should throw an error using createError inside your composable or page. This will cause Nuxt to render the error page before hydration, as you want. The recommended way is to use createError with appropriate statusCode and statusMessage. This is shown in the official docs: Error Handling: createError 2. Custom useFetch Composable
When creating a custom useFetch (like your useApi), you should pass your custom $fetch instance via the $fetch option. This is the correct pattern and is documented in the Nuxt recipes: Custom useFetch Recipe 3. Error Handling in onResponse
Throwing createError inside onResponse is valid, but you should ensure that the error is thrown in a way that Nuxt can catch it during SSR. Your current implementation is close, but you might want to set a more descriptive statusMessage and ensure you are not leaking sensitive data. 4. TypeScript and Error Types
If you want to type your errors, you can use the FetchError type from ofetch, as shown in the docs: Custom useFetch Error Typing Recommendations - Throw createError in onResponse for SSR:
Your pattern is correct. Throwing createError in onResponse will trigger Nuxt's error page during SSR. - Set statusMessage appropriately:
Use response.statusText or a custom message, not the request object. - Use $fetch in useFetch:
Pass your custom $fetch instance via the $fetch option, as you are doing. - Avoid leaking sensitive data:
Do not include the full request object in the error message. Example Adjustment This will ensure that during SSR, if your API returns an error, Nuxt will render error.vue as expected. For more details and examples, see the official Custom useFetch Recipe and Error Handling documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@179707857120395271> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
The AI didn't help me at all so i'll be glad if someone have an idea to help me π
Sorry. Donβt get exactly what you looking for? A full page error?
What I want is if my API throws me a 401 error, i want to show the full page error showing 401 Unauthorized yes
I solved my problem by making my fetch function async with await.
Don't know if i'm doing the right way, but i didn't manage to do better so ... π
I'm closing this issue