N
Nuxt3w ago
Felix

How to intercept/log server side errors?

Hey, I am trying to send notifications to the dev team server whenever there's an unexpected error on the backend (500 errors) but i am not sure how.
2 Replies
kapa.ai
kapa.ai3w ago
Hi @Felix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To intercept and log server-side errors in Nuxt, you have several options. Here's how you can approach this: 1. Use Nitro App Hooks: Nuxt uses Nitro as its server engine, which provides hooks for error handling. You can use the error hook to capture all server-side errors. Here's an example:
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('error', (error, { event }) => {
// Your error logging logic here
console.error('Server error:', error)
// Send notification to dev team
})
})

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('error', (error, { event }) => {
// Your error logging logic here
console.error('Server error:', error)
// Send notification to dev team
})
})

Place this code in a file under the server/plugins/ directory. This hook will be called whenever an error occurs on the server side. Source: Nitro App Hooks (runtime, server-side) 2. Use the onErrorCaptured hook: For Vue-specific errors, you can use the onErrorCaptured hook in a plugin:
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.config.errorHandler = (error, instance, info) => {
// Handle error, e.g. send to a service
}

nuxtApp.hook('vue:error', (error, instance, info) => {
// Handle error, e.g. send to a service
})
})

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.config.errorHandler = (error, instance, info) => {
// Handle error, e.g. send to a service
}

nuxtApp.hook('vue:error', (error, instance, info) => {
// Handle error, e.g. send to a service
})
})

Source: Error Handling 3. Customize the error page: You can create a custom error page by adding an error.vue file in your project's root directory. In this file, you can handle errors and potentially send notifications:
<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object as () => NuxtError
})
// You can add your notification logic here
// For example, send a notification if it's a server error (500)
if (props.error.statusCode === 500) {
// Send notification to dev team
}
</script>

<template>
<div>
<h2>{{ error.statusCode }}</h2>
<!-- Error display logic -->
</div>
</template>

<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object as () => NuxtError
})
// You can add your notification logic here
// For example, send a notification if it's a server error (500)
if (props.error.statusCode === 500) {
// Send notification to dev team
}
</script>

<template>
<div>
<h2>{{ error.statusCode }}</h2>
<!-- Error display logic -->
</div>
</template>

Source: Error Handling Remember to implement the actual notification sending logic (e.g., using an API call to your notification service) in these error handlers. Also, consider using a logging service or error tracking tool that can automatically capture and report server-side errors. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@223579303122567168> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?