0

I am trying to learn AngularJS by building a small webapp with Spring MVC-Spring Security-AngularJS. My UserDetailsService throws a UserNotFoundException while logging in. But i don't know how to handle exceptions in Angular. I have tried to google but could not find simple, complete examples about exception handling.

This is a part of my authentication angular module:

                $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);
                });

How can i reach and catch this specific UserNotFoundException inside the error callback function?

2 Answers 2

2

What i suggest is to create @ExceptionHandler in your controller and in your UserNotFoundException add two more fields (errorMessage and errorCode) for example. then in your angular callback you can check which is the errorCode number.

Good articles:

For advanced error handling I recommend this behaviour : https://github.com/jirutka/spring-rest-exception-handler

Here is a sample fiddle with your case: http://jsfiddle.net/bkUEu/1044/

Sign up to request clarification or add additional context in comments.

2 Comments

I will look at it when i am at home.. looks very promising. Thank you!
no problem, if you want additional help i'm here. feel free to contact me.
0

In your application angularjs try to use this

$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);
                });

Use then and catch function instead of success and error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.