ErrorDisplay
A comprehensive ErrorDisplay component to display user-friendly error messages for various HTTP status codes, enhancing user experience during error scenarios.
Component
Page not found
The page you are looking for does not exist or may have been moved.
Props
| Props | Default | Type |
|---|---|---|
statusCode | required | number |
setPageTitle | true | boolean |
errorMappings | [] | ErrorMapping[] |
showIcon | true | boolean |
showErrorCode | true | boolean |
icon | 'mdi:alert-circle-outline' | string |
backToHomeText | 'Back to home page' | string |
backToHomeIcon | 'mdi:home-outline' | string |
homeRoute | '/' | string |
isFullScreen | true | boolean |
orientation | 'horizontal' | Orientation |
alignContent | 'center' | Align |
isMobileCentered | true | boolean |
Slots
When using the slots, you can customize different parts of the ErrorDisplay component to better fit your application's design and requirements. It will replace the default content for each respective section and ignore the related props.
| Name | Description |
|---|---|
visual-left | Slot for images, illustrations or other visual elements on the left side of the error display. |
visual-right | Slot for images, illustrations or other visual elements on the right side of the error display. |
visual-top | Slot for images, illustrations or other visual elements above the error message. |
description | Slot to provide a custom description or message for the error display. |
actions | Slot to add custom action buttons or links related to the error display. |
<template>
<ErrorDisplay
:statusCode="error?.statusCode ?? -1"
>
<template #visual-top>
<!-- Custom visual content -->
</template>
<template #visual-left>
<!-- Custom visual content -->
</template>
<template #visual-right>
<!-- Custom visual content -->
</template>
<template #description>
<!-- Custom description content -->
</template>
<template #actions>
<!-- Custom action buttons or links -->
</template>
</ErrorDisplay>
</template>
<script setup lang="ts">
const error = useError()
</script>
Nuxt error page
The error.vue page, which is created in the root of the Nuxt project, can contain the ErrorDisplay component to show error messages based on the HTTP status code.
<template>
<ErrorDisplay :statusCode="error?.statusCode ?? -1" />
</template>
<script setup lang="ts">
const error = useError()
</script>
Error mappings can also be customized by passing the errorMappings prop to the ErrorDisplay component.
Usage
statusCode
Sets the HTTP status code for the error display. This code is used to determine the appropriate error message and title to show.
<template>
<ErrorDisplay :statusCode="404" />
</template>
errorMappings
The errorMappings prop allows you to provide custom mappings for different HTTP status codes. Each mapping should include a statusCode, title, description, and optionally an icon.
Provided mappings will override the matching default mappings. The rest of the default mappings will still be available.
<template>
<ErrorDisplay :errorMappings="customErrorMappings" />
</template>
<script setup lang="ts">
const customErrorMappings: ErrorMapping[] = [
{
statusCode: 404,
title: 'Page Not Found',
description: 'The page you are looking for does not exist.',
icon: 'mdi:alert-circle-outline',
},
{
statusCode: 500,
title: 'Internal Server Error',
description: 'Something went wrong on our end. Please try again later.',
icon: 'mdi:server',
},
]
</script>
TypeScript type
type ErrorMapping = {
statusCode: number
title: string
message: string
icon?: string
}
showIcon
Determines whether to display the error icon in the error display component.
<template>
<ErrorDisplay :showIcon="false" />
</template>
setPageTitle
When set to true, the component will update the page title to reflect the error status code and title.
<template>
<ErrorDisplay setPageTitle />
</template>
showErrorCode
Controls the visibility of the error code in the error display.
<template>
<ErrorDisplay :showErrorCode="false" />
</template>
icon
Specifies the icon to be displayed in the error display component. It will be replaced if is a custom icon provided through the errorMappings prop.
<template>
<ErrorDisplay icon="mdi:alert-octagon-outline" />
</template>
backToHomeText
The backToHomeText prop allows you to customize the text displayed on the button that navigates back to the home page.
<template>
<ErrorDisplay backToHomeText="Return to Homepage" />
</template>
backToHomeIcon
The backToHomeIcon prop allows you to set a custom icon for the button that navigates back to the home page.
<template>
<ErrorDisplay backToHomeIcon="mdi:arrow-left-circle-outline" />
</template>
homeRoute
The homeRoute prop specifies the route to which the user will be redirected when they click the "Back to home page" button.
<ErrorDisplay homeRoute="/dashboard" />
</template>
isFullScreen
When set to true, the error display will occupy the full height of the viewport, providing a more immersive experience.
<template>
<ErrorDisplay isFullScreen />
</template>
orientation
The orientation prop allows you to set the layout orientation of the error display content. It uses the Orientation enum.
<template>
<ErrorDisplay :orientation="Orientation.HORIZONTAL" />
</template>
Options
| Value | Description |
|---|---|
HORIZONTAL | Displays content in a horizontal layout |
VERTICAL | Displays content in a vertical layout |
alignContent
The alignContent prop allows you to set the alignment of the content within the error display. It uses the Align enum.
<template>
<ErrorDisplay :alignContent="Align.LEFT" />
</template>
Options
| Value | Description |
|---|---|
LEFT | Aligns content to the left |
CENTER | Centers the content |
RIGHT | Aligns content to the right |
isMobileCentered
When set to true, the content of the error display will be centered on mobile devices for better readability.
<template>
<ErrorDisplay :isMobileCentered="false" />
</template>