-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path01-exception-and-logging.html
74 lines (61 loc) · 2.39 KB
/
01-exception-and-logging.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Exceptions and Logging in AngularJS</title>
<script src="angular.js"></script>
<script type="text/javascript">
'use strict';
(function (app) {
function Controller ($log) {
$log.log('Controller created.');
this.$log = $log;
}
Controller.prototype.warn = function () {
this.$log.warn('Warning generated.');
};
Controller.prototype.error = function () {
this.$log.error('Error generated.');
throw new Error('My custom error.');
};
Controller.prototype.debugger = function () {
debugger;
console.log('Here we are.');
};
Controller.$inject = ['$log'];
app.controller('myCtrl', Controller);
app.factory('$exceptionHandler', ['$injector', function ($injector) {
var rs = null;
return function (exception, cause) {
if (rs === null) {
rs = $injector.get('$rootScope');
}
exception.message = rs.errorList.length + ': ' + exception.message + ' (caused by "' + cause + '")';
rs.errorList.push(exception.message);
throw exception;
};
}]);
app.run(['$rootScope', function ($rootScope) {
$rootScope.errorList = [];
}]);
})(angular.module('angularApp', []));
</script>
</head>
<body data-ng-app="angularApp">
<h1>Angular Debugging and Performance</h1>
<h2>Exceptions and Logging</h2>
<p>Example of using the exception handler and built-in logging. Click the button to see warnings written to the console and errors
logged to the page using the replacement for the exception handling service. Click the debugger button to jump into debug mode.</p>
<div data-ng-controller="myCtrl as ctrl">
<div><button data-ng-click="ctrl.warn()">Warn</button></div>
<div><button data-ng-click="ctrl.error()">Error</button></div>
<div><button data-ng-click="ctrl.debugger()">Debugger</button></div>
</div>
<div>
<b data-ng-if="errorList.length > 0">Error List</b>
<ul>
<li data-ng-repeat="error in errorList">{{error}}</li>
</ul>
</div>
</body>
</html>