【发布时间】:2018-04-13 06:14:29
【问题描述】:
我有一个函数:
buy() {
return new Promise((resolve, reject) => {
this.http.request('http://192.168.1.131:8888/generatetoken.php')
.subscribe(res => {
resolve(res.text());
});
}).then((key) => {
braintree.dropin.create({
authorization: key,
container: '#dropin-container'
}, (createErr, instance) => {
if (createErr) {
// An error in the create call is likely due to
// incorrect configuration values or network issues.
// An appropriate error will be shown in the UI.
console.error(createErr + " createErrrrrrrrrrrrrrrrrr");
return;
}
instance.requestPaymentMethod((requestPaymentMethodErr, payload) => {
if (requestPaymentMethodErr) {
// No payment method is available.
// An appropriate error will be shown in the UI.
console.error(requestPaymentMethodErr + " reqqe");
return;
}
// Submit payload.nonce to your server
});
});
}).catch(error => console.log(error + " eoe"));
}
当它运行时,它会输出:
console.error: ERROR [object Object]
我在哪里可以发现错误?我在then() 之后追赶,但我看不到任何其他可以追赶的地方。请帮忙,谢谢。
【问题讨论】:
-
不确定这是否是您的错误的原因,但需要注意的一点是,使用
.then(success, failure)语法,失败函数不会捕获由成功函数。它只会捕获链中较早抛出的错误。 -
谢谢,我没有完全理解……你是说我的代码末尾的 catch 不会从
.then()块中捕获错误吗?有什么我可以做的吗? -
最后的
.catch将捕获所有内容。我的意思是:.then(() => { throw "error" }, (err) => { // I am never called... }).catch((err) => { // I AM called })我实际上提到它是因为我误读了您的帖子。我原本以为你有两个参数,但你没有。最后的.catch应该捕获所有内容。 -
那么关于我的代码,没有被调用的错误块在哪里?我怎么抓到它?
-
虽然我不熟悉可观察对象,但我也看到您的原始 http 请求没有
reject路径,因此您的捕获永远不会捕获该错误。另外,由于你没有返回braintree.dropin.create创建的promise,如果它抛出错误,catch也不会捕获它。
标签: angular typescript ionic-framework error-handling try-catch