最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to throw custom exception in AngularJS? - Stack Overflow

programmeradmin2浏览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?

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?

Share Improve this question asked Nov 6, 2016 at 4:31 kovackovac 5,38910 gold badges52 silver badges101 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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/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 ments 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
        */
    };
发布评论

评论列表(0)

  1. 暂无评论