Sadeep Sadeep - 29 days ago 6
AngularJS Question

How to throw custom exception in AngularJS?

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?

Answer

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="angularjs@1.4.8" 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
        */
    };
Comments