如何在AngularJS中引发自定义异常?

科瓦奇

我有一个exception.js包含以下服务脚本:

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.
    }
    }
}])

然后,当我Joins在控制器中使用服务而不定义必需的参数时,不会引发我的自定义异常。我在控制台中看不到任何其他错误消息。它根本不显示表格中的任何内容。我做错了什么,还是有更好的方法在Angular中引发自定义异常?

ngCoder

这是您提供的代码的一个有用的插件,它非常适合我!

我假设您是Joins在某个函数中从控制器中调用服务以在发生错误时引发自定义异常。基于以下内容,介绍了如何调用服务的代码。

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>

更新

这是重构代码的小插曲,其中包含有关如何在服务中使用真值条件引发所有不同错误的注释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
        */
    };

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Java变压器内部引发自定义异常在骡子ESB 3.8?

如何从CompletableFuture引发自定义异常?

在Java / Android中引发自定义异常

如何在JAX-WS Web服务上引发自定义错误?

在Java中引发自定义异常

在Java中引发自定义NumberFormatException

从接口实现方法引发自定义异常

使用@ javax.validation.Valid时如何以正确的方式引发自定义异常?

如何在Optional中引发自定义异常

引发自定义异常时如何动态确定方法签名?

如何在Jersey Client中引发自定义异常?

Kotlin-引发自定义异常

如何使用java8 lambda表达式引发自定义检查的异常?

引发自定义的错误/异常

引发自定义异常的正确方法是什么?

如何在C#中的某些路线上引发自定义错误

当我们需要引发自定义异常时,如何防止异常捕获?

如何在Spring Security中引发自定义AccessDeniedException?

在Visual C ++ 2019中引发自定义异常

为什么在ProcessPoolExecutor中引发自定义异常时必须具有默认参数?

如何在Firebase可调用云函数上引发自定义错误?

在WCF的自定义属性中引发自定义异常

从Java ExecutorService捕获并引发自定义异常

如何从Dart扩展dll中引发自定义错误?

Ruby:引发自定义命名空间的异常以错误结尾

引发自己的自定义异常如果使用带注释的MyBatis发生MySql错误

从catch-exception-strategy引发自定义异常

Django auth自定义业务逻辑-如何引发自定义错误消息?

引发自定义错误消息