Hi rani,
Check this example. Now please take its reference and correct your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("MyApp", []);
app.controller('MyController', function ($scope) {
$scope.throwError = function () { throw { message: 'error message' }; };
$scope.throwException = function () { var a = b; };
$scope.throwNestedExceptions = function () {
try { var a = b; }
catch (e) {
try { var c = d; }
catch (ex) { $log.error(e, ex); }
}
};
});
app.provider('$exceptionHandler', {
$get: function (errorLogService) { return (errorLogService); }
});
app.factory('errorLogService', ['$log', '$window', function ($log, $window) {
function log(exception) {
$log.error.apply($log, arguments);
try {
var args = [];
if (typeof arguments === 'object') {
for (var i = 0; i < arguments.length; i++) {
arg = arguments[i];
var exceptionItem = {};
exceptionItem.message = arg.message;
exceptionItem.stack = arg.stack;
args.push(JSON.stringify(exception));
}
}
$log.warn(args.join('\n'));
} catch (loggingError) {
$log.warn("Error logging failed");
$log.log(loggingError);
}
}
return (log);
} ]);
</script>
<div ng-app="MyApp" ng-controller="MyController">
<button type="button" ng-click="throwError()">Throw Error</button>
<button type="button" ng-click="throwException()">Throw Exception</button>
<button type="button" ng-click="throwNestedExceptions()">Throw Nested Exception</button>
</div>
</body>
</html>
Demo