Javascript承诺中的错误处理(抛出错误)
我想对从我正在进行的调用中收到的响应执行一些错误处理,然后在命中特定的空检查时转到catch。大概是这样的:Javascript承诺中的错误处理(抛出错误),javascript,exception,es6-promise,Javascript,Exception,Es6 Promise,我想对从我正在进行的调用中收到的响应执行一些错误处理,然后在命中特定的空检查时转到catch。大概是这样的: fetch('example.json') .then(response => { if (response.data === null) { //go to catch } }) .catch(error => {
fetch('example.json')
.then(response => {
if (response.data === null) {
//go to catch
}
})
.catch(error => {
console.error("error happened", error);
})
fetch('example.json')
.then(response => {
if (!response.ok) {
// (I tend to use an specialized `Error` type here
// More on my anemic blog:
// http://blog.niftysnippets.org/2018/06/common-fetch-errors.html)
throw new Error("HTTP error " + response.status);
}
return response.json();
})
.then(data => {
if (data === null) {
throw new Error("The data is null");
})
// ...do something with `data`...
})
.catch(error => {
console.error("error happened", error);
});
做这样的事情最好的方法是什么?在then块中抛出错误的任何红旗?如果在承诺处理程序中抛出,则拒绝处理程序返回的承诺。因此:
fetch('example.json')
.then(response => {
if (response.data === null) {
throw new Error();
}
})
.catch(error => {
console.error("error happened", error);
})
您抛出的将是catch
处理程序看到的拒绝原因。它不一定是一个错误
,但与同步代码一样,如果是,通常是最好的
但是,请注意A)A获取
响应没有数据
属性,B)您需要检查HTTP成功并解析返回的JSON
你可能想要这样的东西:
fetch('example.json')
.then(response => {
if (response.data === null) {
//go to catch
}
})
.catch(error => {
console.error("error happened", error);
})
fetch('example.json')
.then(response => {
if (!response.ok) {
// (I tend to use an specialized `Error` type here
// More on my anemic blog:
// http://blog.niftysnippets.org/2018/06/common-fetch-errors.html)
throw new Error("HTTP error " + response.status);
}
return response.json();
})
.then(data => {
if (data === null) {
throw new Error("The data is null");
})
// ...do something with `data`...
})
.catch(error => {
console.error("error happened", error);
});
在对问题的评论中,你说: 我希望有一种方法可以检查这个响应对象,而不必触发抛出异常的“极端”措施 你确实有一个结果基本相同的选择:返回一个被拒绝的承诺。下面是我上面的第二个代码块:
fetch('example.json')
.then(response => {
if (!response.ok) {
// (I tend to use an specialized `Error` type here
// More on my anemic blog:
// http://blog.niftysnippets.org/2018/06/common-fetch-errors.html)
return Promise.reject(new Error("HTTP error " + response.status));
}
return response.json();
})
.then(data => {
if (data === null) {
return Promise.reject(new Error("The data is null"));
})
// ...do something with `data`...
})
.catch(error => {
console.error("error happened", error);
});
而对于
抛出
版本,您不必使用错误
,这只是最佳实践。它可以是您想要的任何东西。如果您想要,您可以从promise处理程序中抛出一个错误
对象
fetch('example.json')
.then(response => {
if (response.data === null) {
throw new Error('oopsie');
}
})
.catch(error => {
console.error("error happened", error); // will show "Error: oopsie"
})
如果(response.data==null){throw“Your error”}您可以将此函数包装在一个承诺中,如果您的条件不满足,则可以拒绝它。我还想知道代码实践-在then块中抛出错误是否奇怪,这感觉就像我在用斧头砸我的脚,这样我就可以使用我的医疗保险——我希望有一种方法可以检查这个响应对象,而不必触发抛出异常的“极端”措施。例如,循环中的“continue”(继续)会轻轻地将您移动到下一个循环,而不是执行“break”(中断)然后为循环编写另一个循环。我希望有一种更优雅的方式this@berlin-我们不必触发引发异常的“极端”措施,我已经更新了,为您提供了一个替代方案。它实际上有着相同的结果。(
throw
在JavaScript中并不是很极端。)同意-感觉JavaScript错误远没有java中的错误那么重要。我仍然希望有一个机会让我不必明确地抛出一个伟大的-感谢编辑,我希望我能做这样的事情