Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在AngularJS中捕获/处理后端异常_Javascript_Angularjs_Spring Mvc_Spring Security - Fatal编程技术网

Javascript 如何在AngularJS中捕获/处理后端异常

Javascript 如何在AngularJS中捕获/处理后端异常,javascript,angularjs,spring-mvc,spring-security,Javascript,Angularjs,Spring Mvc,Spring Security,我试图通过使用SpringMVC和SpringSecurityAngularJS构建一个小型Web应用程序来学习AngularJS。My UserDetails服务在登录时引发UserNotFoundException。但我不知道如何处理角度异常。我尝试过谷歌,但找不到关于异常处理的简单、完整的例子 这是我的身份验证模块的一部分: $http.get('user', { headers : headers

我试图通过使用SpringMVC和SpringSecurityAngularJS构建一个小型Web应用程序来学习AngularJS。My UserDetails服务在登录时引发UserNotFoundException。但我不知道如何处理角度异常。我尝试过谷歌,但找不到关于异常处理的简单、完整的例子

这是我的身份验证模块的一部分:

                $http.get('user', {
                    headers : headers
                }).success(function(data) {
                    if (data.name) {
                        auth.authenticated = true;
                    } else {
                        auth.authenticated = false;
                    }
                    callback && callback(auth.authenticated);
                    $location.path(auth.path==auth.loginPath ? auth.homePath : auth.path);
                }).error(function() {
                    auth.authenticated = false;
                    callback && callback(false);
                });

如何在错误回调函数中访问并捕获此特定的UserNotFoundException?

我建议在控制器中创建
@ExceptionHandler
,并在
UserNotFoundException
中再添加两个字段(
errorMessage
errorCode
)。然后在角度回调中,您可以检查哪个是
errorCode
号码

好文章:

对于高级错误处理,我建议采用以下行为:


下面是一个处理您案例的示例:

在您的应用程序中,angularjs尝试使用此

$http.get('user', {
                    headers : headers
                }).then(function(data) {
                    if (data.name) {
                        auth.authenticated = true;
                    } else {
                        auth.authenticated = false;
                    }
                    callback && callback(auth.authenticated);
                    $location.path(auth.path==auth.loginPath ? auth.homePath : auth.path);
                }).catch(function() {
                    auth.authenticated = false;
                    callback && callback(false);
                });

使用then和catch函数,而不是success和error

我在家的时候会看的。。看起来很有希望。非常感谢。没问题,如果你需要更多的帮助,我在这里。请随时与我联系。