【发布时间】:2015-11-16 09:24:43
【问题描述】:
我目前正在编写一个 Angular 应用程序,并且我正在重构我的代码以查看所有错误处理。目前我的 Angular 应用程序使用 Parse.com,我必须在应用程序中管理会话令牌。 Parse.com 有一个关于构建全局实用程序函数的具体建议,所有 Parse 请求错误回调都应调用该函数。我对构建这个全局效用函数很感兴趣,但我不确定它的实现。我猜我应该把它写成一个服务并将它注入我所有的控制器中进行错误检查。这是 Parse.com 的代码
function handleParseError(err) {
switch (err.code) {
case Parse.Error.INVALID_SESSION_TOKEN:
Parse.User.logOut();
... // If web browser, render a log in screen
... // If Express.js, redirect the user to the log in route
break;
... // Other Parse API errors that you want to explicitly handle
}
}
// For each API request, call the global error handler
query.find().then(function() {
...
}, function(err) {
handleParseError(err);
});
https://www.parse.com/docs/js/guide#sessions-handling-invalid-session-token-error
我猜我应该写一个像这样实现错误处理功能的服务
.service(ParseErrorHandler, function(){
return{
function handleParseError(err) {
switch (err.code) {
case Parse.Error.INVALID_SESSION_TOKEN:
Parse.User.logOut();
$scope.go('app.login');
break;
... // Other Parse API errors that you want to explicit handle
...//List of Parse.com Error Codes to reference at link below
...////http://parse.com/docs/dotnet/api/html/
...//T_Parse_ParseException_ErrorCode.htm
}
}
}
})
然后在我的控制器中,我在必要时注入 ParseErrorHandler 服务,并在函数的错误处理程序部分调用该函数
query.find().then(function() {
...
}, function(err) {
ParseErrorHandler.handleParseError(err);
});
这是实施 Parse 的此建议所必需的吗?如果是这样,您也许可以支持我的问题? (不熟悉SO)
【问题讨论】:
标签: angularjs parse-platform error-handling