javascript - How to throw custom exception in AngularJS? -
i have exception.js
script following service:
app.service('exception', function() { this.argumentundefinedexception = function (message) { if (!message) { message = "one or more of arguments undefined!"; } return { name: 'argumentundefinedexception', message: message }; } });
now use in service:
app.service('joins', ['exception', function(exception) { this.leftjoin = function(leftarray, rightarray, joinonleft, joinonright, columnfromrightarray) { if (!leftarray) { throw new exception.argumentundefinedexception("left array not defined leftjoin!"); } else if (!rightarray) { throw new exception.argumentundefinedexception("right array not defined leftjoin!"); } else if (!joinonleft) { throw new exception.argumentundefinedexception("join on left not defined leftjoin!"); } else { // code left join. } } }])
then when use joins
service in controller without defining required arguments, custom exceptions not thrown. not see other error message in console. doesn't show in table. doing wrong or there better way throw custom exceptions in angular?
here working plunker of code have provided , worked me perfectly!
i assumed calling joins
service controller in function throw custom exception if went wrong.based on thatg folowing code on how call 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 arguments 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 not defined leftjoin!"); } else if (!rightarray) { throw new exception.argumentundefinedexception("right array not defined leftjoin!"); } else if (!joinonleft) { throw new exception.argumentundefinedexception("join on left not defined leftjoin!"); } else { // code 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 plunker refactored code comments on how throw 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
Post a Comment