http://localhost:3000
1) Create the layout/error.vue
2) Create the page which will call Axios via Vuex
3) When calling API via Axios, the API return 404/500
The error will pass to the custom Error page.
If user use nuxt-router to change the page (not SSR), the custom error page shown.
But if I refresh and view the SSR Page. The NuxtServerError Page will be shown in dev mode (yarn dev) and the default Server Error page when yarn start.
Here is my Axios Helper
import axios from 'axios'
const myAxios = axios.create({
headers: { 'Accept': 'application/json' }
})
myAxios.interceptors.response.use((response) => {
return response
}, (error) => {
const { response } = error
const { status, statusText } = response
if (process.client) {
$nuxt.error({ message: statusText, statusCode: status })
} else {
// Any solution can access the $nuxt here?
}
return Promise.reject(error)
})
export default myAxios
The page
import myAxios from 'PATH/TO/THIS/HELPER'
..
...
..
myAxios.get('http://example.com')
Any reason you don't use https://github.com/nuxt-community/axios-module/ together with a nuxt plugin, where you have access to the context as well? :thinking:
@manniL Hi, Thanks for reply. My main problem is the custom error page not work when serverside rendering
, so I choose the $nuxt.error()
to force redirect to errorpage.
I can use the axios module with nuxt plugin to fix this problem in this case. But in other files such as Vuex , the action cannot use the nuxt in server side too. the this.$nuxt
will be undefined when server side rendering. If I use throw error
, it will show the default NuxtServerError page.
So I want to know is there any functions can redirect to custom error page or override the default server error page.
@/store/item.js
export const actions = {
fetchItems: async ({ commit }) => {
const items = await myAxois.get('myAPI")
if(items.data.title =='deleted'){
commit('setItems', items.data)
}else{
this.$nuxt.error("deleted Post")..... // Redirect to error page
}
},
}
I have the same problem . How to bring custom error page if initialisation fails even before rendering layout? Also, is it possible to go to error page from plugin e.g. in case if plugin failed to initialise? I am trying all these scenarios in SPA mode and yet didn't manage to get to error page from early failure
Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:
Issues that are labeled as pending
will not be automatically marked as stale.
Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:
Issues that are labeled as pending
will not be automatically marked as stale.
The custom error server page (https://blog.lichter.io/posts/change-the-nuxtjs-server-error-page/) doesn't seem to work for something like Zeit Now, as they just send a "Nuxt.js Internal server error" without seemingly any easy to customize the error, rather than let Nuxt handle the error on its own.
Solved by adding an .htaccess file, in the root folder, with this code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Maybe it can help. :)
Has anyone managed to get this working again, specifically on Zeit Now?
Most helpful comment
@manniL Hi, Thanks for reply. My main problem is
the custom error page not work when serverside rendering
, so I choose the$nuxt.error()
to force redirect to errorpage.I can use the axios module with nuxt plugin to fix this problem in this case. But in other files such as Vuex , the action cannot use the nuxt in server side too. the
this.$nuxt
will be undefined when server side rendering. If I usethrow error
, it will show the default NuxtServerError page.So I want to know is there any functions can redirect to custom error page or override the default server error page.
@/store/item.js