简体   繁体   English

我可以根据响应状态在 axios 帖子中抛出错误吗

[英]Can I throw error in axios post based on response status

Is it possible to throw an error on purpose inside the.then() block in axios?是否可以在 axios 的 .then() 块内故意抛出错误? For instance, if the api responds with 204 status code, could I throw an error and run the catch block?例如,如果 api 响应 204 状态代码,我可以抛出错误并运行 catch 块吗?

For example:例如:

 axios.post('link-to-my-post-service', { json-input }).then(response => { if (response.status === 200) { //proceed... } else { // throw error and go to catch block } }).catch(error => { //run this code always when status;==200 })

EDIT编辑

I tried this, but it didn't work:我试过这个,但没有奏效:

 var instance = axios.create({ validateStatus: function (status) { return status == 200; } }); axios.post('link-to-my-post-service', {input: myInput}, instance).then(response => { dispatch({ type: "FETCH_SUCCESS", payload: response.data }); }).catch(error => { dispatch({ type: "FETCH_FAILED", payload: error }); });

When I get a status code 204, still the executed block is then() block instead of the catch block.当我得到状态码 204 时,执行的块仍然是 then() 块而不是 catch 块。

EDIT 2编辑 2

The correct answer using Ilario's suggestion is this:使用 Ilario 的建议的正确答案是:

 var instance = axios.create({ validateStatus: function (status) { return status == 200; } }); instance.post('link-to-my-post-service', {input: myInput}).then(response => { dispatch({ type: "FETCH_SUCCESS", payload: response.data }); }).catch(error => { dispatch({ type: "FETCH_FAILED", payload: error }); });

Now when the status code is not equal to 200 the catch block code is executed.现在,当状态码不等于 200 时,将执行 catch 块代码。

If you give a look at the GitHub Project Page you will notice following option description.如果您查看 GitHub项目页面,您会注意到以下选项描述。

 /* `validateStatus` defines whether to resolve or reject the promise for a given * HTTP response status code. If `validateStatus` returns `true` (or is set to `null` * or `undefined`), the promise will be resolved; otherwise, the promise will be */ rejected. validateStatus: function (status) { return status >= 200 && status < 300; // default },

So you could create an Instance with your own configuration.因此,您可以使用自己的配置创建一个实例。

 var instance = axios.create({ validateStatus: function (status) { return status == 200; }, });

You could also set defaults.您还可以设置默认值。 These will be applied to every request.这些将应用于每个请求。

 axios.defaults.validateStatus = () => { return status == 200; };

UPDATE 1更新 1

To set the config only on a specific operation you could replace "config" with your desired values or methods.要仅在特定操作上设置配置,您可以将“config”替换为您想要的值或方法。

 axios.post(url[, data[, config]])

UPDATE 2更新 2

I tried this, but it didn't work.我试过这个,但它没有用。

You cannot pass the instance to axios.post().您不能将实例传递给 axios.post()。 You must call post on the new instance.您必须在新实例上调用 post。

 var instance = axios.create({ validateStatus: function (status) { return status == 200; } }); instance.post('url', data, config);

Thank you very much for your suggestions.非常感谢您的建议。 The answer was simpler than I expected.答案比我预期的要简单。

I didn't want to set any default options to change the behavior of axios, so I just tried something like the code below, and it worked.我不想设置任何默认选项来更改 axios 的行为,所以我只是尝试了类似下面的代码,它确实有效。 Every time the code throw new Error("Error");每次代码throw new Error("Error"); is executed, the catch block code is executed after that.执行后,将执行 catch 块代码。

 axios.post('link-to-my-post-service', { json-input }).then(response => { if (response.status === 200) { //proceed... } else { // throw error and go to catch block throw new Error("Error"); } }).catch(error => { //when throw "Error" is executed it runs the catch block code console.log(error) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM