2016-11-06 21 views
0

私は以下のサービスをexception.jsスクリプトを持っています、AngularJSでカスタム例外をスローする方法は?

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サービスを使用する場合:

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

今、私は別のサービスでこれを使用私のカスタム例外はスローされません。コンソールに他のエラーメッセージが表示されません。テーブルには何も表示されません。私は何が間違っているのですか、または角でカスタム例外をスローする良い方法がありますか?

答えて

4

あなたが提供したコードのplunkerがここにあります。それは私にとって完璧に機能しました!

何か問題が発生した場合に、関数のコントローラから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>

UPDATEここ

は、サービス中の真実の条件を使用して、すべての異なるエラーをスローする方法についてのコメントをリファクタリングコードのplunkerある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

あなたが 'Joins.LEFTJOIN();'で引数を送らなくても、関数は呼び出され、あなたの偽の条件をチェックすることで!未定義の引数をtrueにし、最初のif節が実行されます –

+0

これはうまくいきます。最初の引数を渡して、2番目の引数をfalseに設定しようとしました。その後、2番目の例外はスローされません。しかし、あなたは私のために何かを明らかにしました。私は最初は違う行動を期待していました。 – swdon

+0

だから助けてくれましたか?あなたはこの答えを受け入れることができますか?または私は何かをさらに改善すべきですか? –

関連する問題