I want to handle axios errors unifiedly when the response's status code is not 2XX or 3XX .
For example, requests for captchas, the backend will return 429 status code if there are too many requests in a short time sent from one client.
import axios from 'axios';
class Captcha{
static async store(){
const { data } = await axios.post(`${process.env.API_URL}/auth/captchas`);
return data;
}
}
export default Captcha;
import axios from 'axios';
// after getting response
axios.interceptors.response.use(
response => {
return response;
},
error => {
// handle errors unifiedly here
handleErrors(error);
}
);
in nuxt.config.js
plugins:[
'~/plugins/axios-interceptor'
],
<template>
...
<img :src="captcha.image_content" @click="refreshCaptcha()">
...
</template>
import Captcha from '@/models/Captcha';
export default{
async asyncData(){
const captcha = await Captcha.store();
return {
captcha: captcha
}
},
methods:{
async refreshCaptcha(){
this.captcha = await Captcha.store();
}
},
...
}
But it can't work as I expected.
First, I don't want to have the default errors handler by Nuxt.js
Second, it can't handle errors happened in node.js
, I want to render the page normally when errors occurred
How can I solve it? I don't want to use callback instead of Promise or async function, because the Nuxt.js Official said they will not support it any longer in the future.
Thanks a lot!
The asyncData command runs on the server side and you can not use the methods and services you wrote on the client side in asyncData. In fact, the contents of asyncData are called before your project loads.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.