Javascript 使用JSON对象进行Duck键入-try/except?
在AngularJS中,我有一个发出的api请求,并返回一个JSON。JSON存储为objectJavascript 使用JSON对象进行Duck键入-try/except?,javascript,angularjs,error-handling,duck-typing,Javascript,Angularjs,Error Handling,Duck Typing,在AngularJS中,我有一个发出的api请求,并返回一个JSON。JSON存储为objectdata,我使用data.Automation.Status检查Status中的字符串 有一些JSON错误可能会增加(在http 200成功并成功返回JSON后): 整个JSON可能返回一个空字符串“” 数据JSON对象可能存在,但自动化JSON对象可能未定义 对象的属性状态自动化可能未定义或为空字符串 来自python,所有这些可能的情况都可以在try/except块中轻松处理 Try: do
data
,我使用data.Automation.Status
检查Status
中的字符串
有一些JSON错误可能会增加(在http 200成功并成功返回JSON后):
状态
自动化可能未定义或为空字符串Try:
do something with JSON
except (blah, blah)
don't error out because JSON object is broken, but do this instead
我看到angular有$errorHandler服务,可以使用自定义处理程序进行修改。但我不确定这是否可以用在我正在寻找的duck类型中
我怎样才能用AngularJS输入duck?具体来说,对于上面列表中提到的JSON对象错误场景
目前我如何使用数据.自动化.状态
:
if (iteration < Configuration.CHECK_ITERATIONS && data.Automation.Status !== "FAILED") {
iteration++;
return $timeout((function() {
return newStatusEvent(eventId, url, deferred, iteration);
}), Configuration.TIME_ITERATION);
} else {
err = data.Automation.StatusDescription;
return deferred.reject(err);
}
if(迭代
取决于您想要得到的复杂程度。如果你想用咖啡脚本,有一个很好的?运算符:json.knownToExists.maybeExists?.maybeAlsoExists?()
将永远不会出错,并且仅在存在maybeAlsoExists时调用它
否则,您可以使用检查类型:
foo = {a: 1, b: 2}
typeof foo === 'object'
typeof foo.bar === 'undefined'
我的印象是javascript中的try/catch块相对昂贵。因此,您可能希望手动测试json,特别是如果服务的不同返回值是您所谓的“正常”响应。依我看,异常应该用于异常事件,而不是健全性检查。这取决于您想要得到的复杂程度。如果你想用咖啡脚本,有一个很好的?运算符:
json.knownToExists.maybeExists?.maybeAlsoExists?()
将永远不会出错,并且仅在存在maybeAlsoExists时调用它
否则,您可以使用检查类型:
foo = {a: 1, b: 2}
typeof foo === 'object'
typeof foo.bar === 'undefined'
我的印象是javascript中的try/catch块相对昂贵。因此,您可能希望手动测试json,特别是如果服务的不同返回值是您所谓的“正常”响应。例外,依我看,应该用于例外事件,而不是健全性检查。以下是我如何找到解决同一问题的方法。
它使它保持最小,并且所有测试都分组在一个块中
$http.get('/endpoint1').success(function(res) {
try {
// test response before proceeding
if (JSON.stringify(res).length === 0) throw 'Empty JSON Response!';
if (!res.hasOwnProperty('Automation')) throw 'Automation object is undefined!';
if (!res.Automation.hasOwnProperty('Status') || res.Automation.Status !== 'SUCCESS')
throw 'Automation Status Failed!';
} catch (err) {
// handle your error here
console.error('Error in response from endpoint1: ' + err);
}
// if your assertions are correct you can continue your program
});
下面是我如何找到解决同一问题的方法。
它使它保持最小,并且所有测试都分组在一个块中
$http.get('/endpoint1').success(function(res) {
try {
// test response before proceeding
if (JSON.stringify(res).length === 0) throw 'Empty JSON Response!';
if (!res.hasOwnProperty('Automation')) throw 'Automation object is undefined!';
if (!res.Automation.hasOwnProperty('Status') || res.Automation.Status !== 'SUCCESS')
throw 'Automation Status Failed!';
} catch (err) {
// handle your error here
console.error('Error in response from endpoint1: ' + err);
}
// if your assertions are correct you can continue your program
});
处理这种情况的最好方法是使用$parse,因为它可以很好地处理未定义的问题。你可以这样做:
$http.get('/endpoint1').success(function(res) {
try {
// test response before proceeding
if (angular.isUndefined($parse('Automation.Status')(res))) {
throw 'Automation Status Failed!';
}
} catch (err) {
// handle your error here
console.error('Error in response from endpoint1: ' + err);
}
// if your assertions are correct you can continue your program
});
您可以从中看到$parse如何处理您的场景。处理这种情况的最佳方法是使用$parse,因为它可以很好地处理未定义的场景。你可以这样做:
$http.get('/endpoint1').success(function(res) {
try {
// test response before proceeding
if (angular.isUndefined($parse('Automation.Status')(res))) {
throw 'Automation Status Failed!';
}
} catch (err) {
// handle your error here
console.error('Error in response from endpoint1: ' + err);
}
// if your assertions are correct you can continue your program
});
您可以从中看到$parse如何处理您的场景。与问题无关,但我发现$timeout的使用非常奇怪:您不应该使用deferred.resolve()来代替吗?您知道您可以在JavaScript中执行完全相同的操作,不是吗?与问题无关,但是我发现$timeout的用法非常奇怪:你不应该用deferred.resolve()来代替吗?你知道你可以在JavaScript中做完全相同的事情,不是吗?我喜欢这个,因为它看起来最干净。还有什么我可以给你额外的赏金点数的吗?不确定,我很高兴我的回答是有用的我喜欢这个,因为它看起来是最干净的。还有什么我可以给你额外的赏金点数的吗?不确定,我很高兴我的回答是有用的D