0

I have a exception.js script with the following service:

app.service('Exception', function() {
this.ArgumentUndefinedException = function (message) {
    if (!message) {
        message = "One or more of the arguments are undefined!";
    }
    return {
        name: 'ArgumentUndefinedException',
        message: message
    };
  }
});

Now I use this in another service:

app.service('Joins', ['Exception', function(Exception) {
this.LEFTJOIN = function(leftArray, rightArray, joinOnLeft, joinOnRight, columnFromRightArray) {
    if (!leftArray) {
        throw new Exception.ArgumentUndefinedException("Left Array is not defined for LEFTJOIN!");
    } else if (!rightArray) {
        throw new Exception.ArgumentUndefinedException("Right Array is not defined for LEFTJOIN!");
    } else if (!joinOnLeft) {
        throw new Exception.ArgumentUndefinedException("Join On Left is not defined for LEFTJOIN!");
    } else {    
      // code to do the left join.
    }
    }
}])

Then when I use the Joins service in a controller without defining the required arguments, my custom exceptions are not thrown. I do not see any other error message in the console. It simply doesn't show anything in the table. What am I doing wrong or is there a better way to throw custom exceptions in Angular?

1 Answer 1

4

Here is a working plunker of the code you have provided and it worked for me perfectly!

I assumed that you are calling your Joins service from a controller in a function to throw a custom exception if something went wrong.Based on thatg folowing is the code on how you call your Service.

var app = angular.module("app", []);
app.controller('Ctrl', function($scope, $http, Joins) {


    $scope.throwException = function() {
        Joins.LEFTJOIN(false);
    };
});

var app = angular.module("app", []);
app.controller('Ctrl', function($scope, $http, Joins) {


    $scope.throwException = function() {
        Joins.LEFTJOIN(false);
    };
});

app.service('Exception', function() {
    this.ArgumentUndefinedException = function(message) {
        if (!message) {
            message = "One or more of the arguments are undefined!";
        }
        return {
            name: 'ArgumentUndefinedException',
            message: message
        };
    }
});


app.service('Joins', ['Exception', function(Exception) {
    this.LEFTJOIN = function(leftArray, rightArray, joinOnLeft, joinOnRight, columnFromRightArray) {
        if (!leftArray) {
            throw new Exception.ArgumentUndefinedException("Left Array is not defined for LEFTJOIN!");
        } else if (!rightArray) {
            throw new Exception.ArgumentUndefinedException("Right Array is not defined for LEFTJOIN!");
        } else if (!joinOnLeft) {
            throw new Exception.ArgumentUndefinedException("Join On Left is not defined for LEFTJOIN!");
        } else {
            // code to do the left join.
        }
    }
}]);
<html>

  <head>
    <script data-require="[email protected]" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
  
  </head>

 <body ng-app="app" ng-controller="Ctrl">

<button ng-click="throwException()">Exception</button>
</body>

</html>

UPDATE

Here is the plunker for refactored code with comments on how to throw all different errors using truth conditions in service Joins

 $scope.throwException = function() {
        Joins.LEFTJOIN(true, false, false); //throw no left array
       /* Joins.LEFTJOIN(false, true, false); //throw no right array
        Joins.LEFTJOIN(false, false, true); //throw no join on left array
        Joins.LEFTJOIN(); //throw default exception
        */
    };
Sign up to request clarification or add additional context in comments.

5 Comments

Even if you dont send any argument in the Joins.LEFTJOIN(); the function gets called and as your checking for false condition with ! that would turn the undefined arguments to true and the first if clause is executed
Yeah, this works. I tried passing the first argument and setting the second argument to false. Then it doesn't throw the second exception. However, you answer did clarify somethings for me. I was expecting a different behaviour at first.
so did it helpu you ? can u accept this answer ? or should I improve anything more ?
Can you add a comment in the plunker on how I can the 2nd and 3rd exceptions to be thrown too? When I tried it, I couldn't get it to work. I will accept your answer after that.
I would suggest you to check for truth conditions (positive scenarios) instead of false cond like !leftArray because even null and undefined in this case will be true

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.