Skip to main content
InterviewHubPRO
Section Overview
Q21

Question 21 of 50

Part 2: Core Architecture & Data Fetching (Mid-Level)

42%
Nuxt & Universal SSR (50 Qs)•Part 2: Core Architecture & Data Fetching (Mid-Level)

How do you implement robust error handling with `error.vue` and `createError()`?

senior LevelCode ExampleSystem DesignVue & NuxtHTTP & REST APIs

In Simple Words: Nuxt provides full-stack error handling across both server and client lifecycles:

  1. Backend / API: Throwing createError({ statusCode: 404, statusMessage: 'Not Found' }) halts execution and sends structured JSON or triggers the error page.
  2. Frontend UI: If an uncaught error occurs during page execution, Nuxt replaces the application with error.vue located in the project root.
  3. Clearing Errors: clearError({ redirect: '/' }) resets error state and navigates back to safe routes.
vue
<!-- error.vue (in root) -->
<script setup lang="ts">
import type { NuxtError } from '#app';

const props = defineProps<{
  error: NuxtError;
}>();

const handleClearError = () => clearError({ redirect: '/' });
</script>

<template>
  <div class="error-container">
    <h1>Error {{ error.statusCode }}</h1>
    <p>{{ error.message }}</p>
    <button @click="handleClearError">Return to Home</button>
  </div>
</template>