抛出错误的顺序?
Order of thrown errors?
我有一个函数,在出错时(例如 "timeout")应该抛出一个错误,我在承诺链的末尾捕获了这个错误。
var createOrder = function (id) {
Utility.toggleProgressBar();
return $.ajax({
url: 'http://' + AppVar.ServerUrlWithPort + '/restapi/CreateOrder',
data: JSON.stringify({
'SessionId': AppVar.SessionId,
'Idrmtreq': id
}),
}).then(function (response) {
if (response.ResultCode === '0') {
return response;
} else {
throw new Error($.i18n('Error-RetrivingDataProblem'));
}
}).fail(function (x, t, m) {
if (t === "timeout") {
throw new Error($.i18n('Error-Timeout')); //code reaches here, where Chrome debugger says that this error was left Uncaught
//for the record, I checked whether the translation function could be the problem and it doesn't work even when I do 'throw "timeout";'
} else {
throw new Error($.i18n('Error-ConnError'))
}
}).catch(function (error) {
//error == {"readyState":0,"status":0,"statusText":"timeout"}
ErrorManager.displayError(error);
return;
}).always(function () {
Utility.toggleProgressBar();
})
}
具体来说,我遇到了超时问题。代码到达投掷。我扔的东西实际上没有被捕获,但是有东西被扔了。 Catch 捕获包含此对象 {"readyState":0,"status":0,"statusText":"timeout"}
.
的错误
我不明白。什么在扔它?
切勿使用 done
或 fail
。他们不链接(也不捕获异常)。
fail()
调用 returns 原始承诺,在您的情况下,这意味着原始错误刚刚落空。您可以使用 .catch()
instead, but notice that chaining .then(…).catch(…)
来处理 Error-RetrivingDataProblem。相反,你会想要使用
return $.ajax({
url: 'http://' + AppVar.ServerUrlWithPort + '/restapi/CreateOrder',
data: JSON.stringify({
'SessionId': AppVar.SessionId,
'Idrmtreq': id
}),
}).then(function (response) {
if (response.ResultCode === '0') {
return response;
} else {
throw new Error($.i18n('Error-RetrivingDataProblem'));
}
}, function(x, t, m) { /*
^^^ */
throw new Error($.i18n(t === "timeout" ? 'Error-Timeout' : 'Error-ConnError'));
}).catch(function (error) {
ErrorManager.displayError(error);
return;
})
我有一个函数,在出错时(例如 "timeout")应该抛出一个错误,我在承诺链的末尾捕获了这个错误。
var createOrder = function (id) {
Utility.toggleProgressBar();
return $.ajax({
url: 'http://' + AppVar.ServerUrlWithPort + '/restapi/CreateOrder',
data: JSON.stringify({
'SessionId': AppVar.SessionId,
'Idrmtreq': id
}),
}).then(function (response) {
if (response.ResultCode === '0') {
return response;
} else {
throw new Error($.i18n('Error-RetrivingDataProblem'));
}
}).fail(function (x, t, m) {
if (t === "timeout") {
throw new Error($.i18n('Error-Timeout')); //code reaches here, where Chrome debugger says that this error was left Uncaught
//for the record, I checked whether the translation function could be the problem and it doesn't work even when I do 'throw "timeout";'
} else {
throw new Error($.i18n('Error-ConnError'))
}
}).catch(function (error) {
//error == {"readyState":0,"status":0,"statusText":"timeout"}
ErrorManager.displayError(error);
return;
}).always(function () {
Utility.toggleProgressBar();
})
}
具体来说,我遇到了超时问题。代码到达投掷。我扔的东西实际上没有被捕获,但是有东西被扔了。 Catch 捕获包含此对象 {"readyState":0,"status":0,"statusText":"timeout"}
.
我不明白。什么在扔它?
切勿使用 done
或 fail
。他们不链接(也不捕获异常)。
fail()
调用 returns 原始承诺,在您的情况下,这意味着原始错误刚刚落空。您可以使用 .catch()
instead, but notice that chaining .then(…).catch(…)
来处理 Error-RetrivingDataProblem。相反,你会想要使用
return $.ajax({
url: 'http://' + AppVar.ServerUrlWithPort + '/restapi/CreateOrder',
data: JSON.stringify({
'SessionId': AppVar.SessionId,
'Idrmtreq': id
}),
}).then(function (response) {
if (response.ResultCode === '0') {
return response;
} else {
throw new Error($.i18n('Error-RetrivingDataProblem'));
}
}, function(x, t, m) { /*
^^^ */
throw new Error($.i18n(t === "timeout" ? 'Error-Timeout' : 'Error-ConnError'));
}).catch(function (error) {
ErrorManager.displayError(error);
return;
})