dev-resources.site
for different kinds of informations.
Nuxt3 : API error handling
Published at
3/13/2024
Categories
nuxt
errors
api
nuxt3
Author
v0id-4lps
Author
9 person written this
v0id-4lps
open
Hello World
First post here, hello world!
Context
- Nuxt 3.10.3
- Vue 3.4.15
In Nuxt 3.10.3 you can't retrieve custom error message properly.
If you write a custom error message you cannot be able to retrieve it without a little but dirty workaround.
Example
Here is a short example :
- A simple API event handler throwing a basic error
- A simple page with 2 actions:
- getting an error with
$fetch()
- getting an error with
useFetch()
- and displaying the result
- getting an error with
# /pages/error.vue
<template>
<p><button @click="onClickFetch">GetError with $fetch()</button></p>
<p><button @click="onClickUseFetch">GetError with useFetch()</button></p>
<pre v-if="myError">
- statusCode: {{ myError.statusCode }}
- statusMessage: {{ myError.statusMessage }}
- message: {{ myError.message }}
- data: {{ myError.data }}
</pre>
</template>
<script setup lang="ts">
const myError = useState('myError')
const onClickFetch = async e => {
myError.value = null;
try {
const data = await $fetch('/api/error')
} catch (error) {
myError.value = error
}
}
const onClickUseFetch = async e => {
myError.value = null;
const { data, error, execute, pending, refresh, status } = await useFetch('/api/error')
myError.value = error
}
</script>
# /server/api/error.ts
export default defineEventHandler(async event => {
throw createError({
statusCode: 500,
statusMessage: 'MyError statusMessage (éàè)',
message: 'MyError message (éàè)'
data: {
data: {
message: `MyError data message (éàè)`,
},
},
})
})
Result
- statusCode: 500
- statusMessage: Internal Server Error
- message: [GET] "/api/error": 500 Internal Server Error
- data:
{
data: {
message: `MyError data message (éàè)`,
},
}
As you can see, .statusMessage
and .message
are not returning your custom message.
Workaround
Dirty workaround : use .data
to pass your custom error message.
# /server/api/error.ts
export default defineEventHandler(async event => {
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
// Here we have to define the `data` property
data: {
message: `My custom error message`,
},
})
})
<template>
<div v-if="myError">
<!-- Here we have to use .data.data.message -->
<p>{{ myError.data.data.message }}</p>
</div>
</template>
<script setup lang="ts">
const myError = useState('myError')
try {
const data = await $fetch('/api/error')
} catch (error) {
myError.value = error
}
</script>
It's dirty because your have to do 2 things :
- writing the
data
block on every error you want to throw - using
.data.data.message
every time you want to display your message
Not found a better solution from now.
If you know a better solution, please, let us know in comment.
nuxt3 Article's
30 articles in total
Renderización Dinámica de Componentes en Vue 3 y Nuxt 3: Guía Práctica y Caso Real
read article
Nuxt3 CSR Background Image Lazy loading
read article
DartsMash: a platform for darts enthusiasts
read article
Nuxt3 : limitation on Layers & Modules
read article
Using postgresql with nuxt3 by ORM(sequelize)
read article
Nuxt3 : API error handling
currently reading
Nuxt Icon
read article
Integrating Nuxt 3 with Recaptcha v3 for Token Handling 🤖🔐
read article
Nuxt 3 Starter
read article
Nuxt 3 Builder: A Tailored Editor for Developers
read article
Build an X clone w/ Nuxt UI
read article
Nuxt3 Form with Feedback
read article
Custom 404 Page in Nuxt
read article
Nuxt 3 authentication with pinia
read article
Nuxt 3, UnoCSS, and Preset rem to px
read article
Configuração e instalação do Nuxt 3
read article
The DevOnly component in Nuxt 3: A developer's best friend
read article
Storyblok Nuxt 3 news 🗞
read article
Implementing OpenID Connect (OIDC) Authentication with Nuxt 3
read article
Authentication in Nuxt 3
read article
Adding ESLint and Prettier to Nuxt 3 ✨ (2024)
read article
API Management in Nuxt 3 with TypeScript
read article
Docker and Nuxt 3
read article
Pinia and Nuxt 3
read article
Adding Pinia to Nuxt 3 🍍 (2023)
read article
Adding Vitest to Nuxt 3 ⚡ (2023)
read article
Adding Tailwind CSS to Nuxt 3 🍃 (2023)
read article
How I set up eslint in my projects
read article
How to create and minify Vuetify 3 Nuxt 3 project bundle
read article
Build an Intercom clone in Nuxt.js - Part Two
read article
Featured ones: