We can use the error method from the context object to call the Nuxt default error page and display the error. You can pass the error code and message through the default params.statusCode and params.message properties:
// pages/users/error.vue
export default {
asyncData ({ error }) {
return error({
statusCode: 404,
message: 'User not found'
})
}
}
If you want to change the default properties that you pass to the error method, you can create a custom error page, which you learned about in Chapter 4, Adding Views, Routes, and Transitions. Let's create these custom error properties and layout in the following steps:
- Create a page that you want to throw the custom properties to:
// pages/users/error-custom.vue
export default {
asyncData ({ error }) {
return error({
status: 404,
text: 'User not found'
})
}
}
- Create a custom error page in the /layouts/ directory:
// layouts/error.vue
<template>
<div>
...