-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporting.js
83 lines (66 loc) · 2.76 KB
/
reporting.js
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
75
76
77
78
79
80
81
82
'use strict';
/**
* Report angular errors to server. Based on http://www.bennadel.com/blog/2542-Logging-Client-Side-Errors-With-AngularJS-And-Stacktrace-js.htm
*
* Requires stacktrace.js and jQuery included on the page
**/
var errorReportingModule = angular.module('errorReporting', []);
// wrapper around stacktrace.js
errorReportingModule.factory( 'stacktraceService', ['$log', function($log) {
if (printStackTrace === undefined){
$log.error('stacktrace.js was not found on this page');
}
// "printStackTrace" is a global object.
return({
print: printStackTrace
});
}]
);
// error reporting provider
errorReportingModule.provider('ErrorReport', [function () {
this.reportUrl = '';
this.$get = ['$log', '$window', 'stacktraceService', function($log, $window, stacktraceService){
var url = this.reportUrl;
return {
report: function( exception, cause ) {
// try and log the error the server.
try {
if(!url){
$log.error('Can\'t log error to server: reportUrl not set\n');
return;
}
var errorMessage = exception.toString();
var stackTrace = stacktraceService.print({ e: exception });
var userAgent = $window.navigator.userAgent;
// Log the JavaScript error to the server.
// using jQuery ajax call instead of AngularJS $http, due to circular dependencies ($http depends on $exceptionHandler)
$.ajax({
type: "POST",
url: url,
contentType: "application/json",
data: angular.toJson({
errorUrl: $window.location.href,
errorMessage: errorMessage,
stackTrace: stackTrace,
cause: ( cause || "" ),
userAgent: userAgent
})
});
} catch ( loggingError ) {
// For Developers - log the log-failure.
$log.warn( "Error logging failed" );
$log.log( loggingError );
}
}
}
}]
}]);
// decorate default AngularJS exception handler
errorReportingModule.config(['$provide', function($provide) {
$provide.decorator("$exceptionHandler", ['$delegate', 'ErrorReport', function($delegate, ErrorReport) {
return function(exception, cause) {
$delegate(exception, cause);
ErrorReport.report(exception, cause);
};
}]);
}]);