Is there a way to NOT show an HTML error from an API?
The recommended way of handling errors in Nitro/API-land is to use
createError
. When hit directly, these show a full html error page, or if hit with accept: application/json
, show a json payload with stack info.
I'd like to return a simple message on-error vs all that. It would seem the only option I have is to use setResponseStatus
and return whatever
directly from the api vs passing something to createError
.
IMHO, no matter what, an api shouldn't be returning full page html errors.4 Replies
I just put this together for showing a toast during a fetch error. You would still need to handle the fetch calls with try/catch, as i do in components/SomeComp.vue
GitHub
GitHub - marr/nuxt-error-toast: Example showing how to display a to...
Example showing how to display a toast when there is a fetch error. - marr/nuxt-error-toast
It's using radix-ui for the toast.. i never see examples of showing toasts like this with errors, so im not sure if there is a better approach
Hi Josh. I recently went through same frustration.
Here’s how I handle it:
Then I use it something like this
But yes, as you mentioned I do set the responseStatus. The createError is a suboptimal solution imo.