try-catch用于后端服务的调用,因为它有可能返回空响应。
当响应为空时,错误标记为:
“ ReferenceError:未定义modelInfo”。
可以预料到API可能会关闭并且发生ReferenceError的事实。
为了处理此错误,将代码包装在try-catch中。
我希望使用try-catch的行为是请求服务信息时会出错,然后跳转到“ catch”,运行该代码。
捕获代码没有运行?
var activeMessage = view.querySelector('#activeMessage');
var html = '';
try {
//This call to the backend could throw a ReferenceError
ApiClient.getJSON(ApiClient.getUrl("VeraModelInfo")).then(function (modelInfo) {
html += modelInfo.Name;
html += '<br />';
html += modelInfo.InternalIp;
html += '<br />';
html += '<span id="icnConnectedContainer" style="color:rgb(82,181,75);font-size: 73%; opacity:0;">';
html += '<i class="md-icon" style="padding-bottom: 0.5%;">';
html += 'check_circle_outline';
html += '</i>';
html += ' Connected';
html += '</span > ';
activeMessage.style.opacity = 0;
activeMessage.innerHTML = html;
activeMessage.style.color = '#303030';
}
catch (error) { //This catch doesn't run after ReferenceError
html += '<span style="color: red" >';
html += '<i class="md-icon">';
html += 'circle_remove';
html += '</i>';
html += '<span>';
html += ' No Vera Home Automation Device Detected!';
activeMessage.innerHTML = html;
activeMessage.style.color = 'red';
activeMessage.style.opacity = 1;
activeMessage.style.display = 'block';
view.querySelector('#deviceModel').style.display = "none";
}
意识到错误并不总是例外...
在请求中出现ReferenceError时如何引发异常以运行捕获代码。
还是认识到ReferenceError可以在另一个函数中处理客户端布局代码?
答案 0 :(得分:0)
我最终(由于@Pointy的建议)写了这篇文章,并掌握了Promises的想法,该想法奏效了。
var activeMessage = view.querySelector('#activeMessage');
var html = '';
return new Promise(() => {
ApiClient.getJSON(ApiClient.getUrl("VeraModelInfo")).then(function (modelInfo) {
html += modelInfo.Name;
html += '<br />';
html += modelInfo.InternalIp;
html += '<br />';
html += '<span id="icnConnectedContainer" style="color:rgb(82,181,75);font-size: 73%; opacity:0;">';
html += '<i class="md-icon" style="padding-bottom: 0.5%;">';
html += 'check_circle_outline';
html += '</i>';
html += ' Connected';
html += '</span > ';
activeMessage.style.opacity = 0;
activeMessage.innerHTML = html;
activeMessage.style.color = '#303030';
activeMessage.animate({
transform : ['translateX(-200px)', 'translateX(0)'],
opacity : [0, 1]
},
{
duration: 300,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 525
});
var icnConnectedContainer = view.querySelector('#icnConnectedContainer');
icnConnectedContainer.animate({
opacity : [0, 1]
},
{
duration: 320,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 880
});
// Place the image for the users MiCasaVerde Device in an image.
var deviceImg = view.querySelector('#deviceModel');
deviceImg.src = modelInfo.ImageUrl;
deviceImg.style.opacity = 0;
deviceImg.style.display = 'block';
deviceImg.animate({
transform : ['scale(0.89)', 'scale(1)'],
opacity : [0, 1]
},
{
duration: 220,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 120
});
if (config.SavedDeviceProfiles) {
config.SavedDeviceProfiles.forEach(function (device) {
view.querySelector('#clientProfiles').innerHTML += (getClientHtml(device));
});
}
}, () => {
html += '<span style="color: red" >';
html += '<i class="md-icon">';
html += 'error';
html += '</i>';
html += '<span>';
html += ' No Vera Home Automation Device Detected!';
activeMessage.innerHTML = html;
activeMessage.style.color = 'red';
activeMessage.style.opacity = 1;
activeMessage.style.display = 'block';
view.querySelector('#deviceModel').style.display = "none";
});