|
16 | 16 | (function(exports) { |
17 | 17 | "use strict"; |
18 | 18 |
|
| 19 | + /** |
| 20 | + * URL endpoint of the Stackdriver Error Reporting report API. |
| 21 | + */ |
| 22 | + var baseAPIUrl = "https://clouderrorreporting.googleapis.com/v1beta1/projects/"; |
| 23 | + |
19 | 24 | /** |
20 | 25 | * An Error handler that sends errors to the Stackdriver Error Reporting API. |
21 | 26 | */ |
22 | | - var StackdriverErrorReporting = function() {}; |
23 | | - exports.StackdriverErrorReporting = StackdriverErrorReporting; |
| 27 | + var StackdriverErrorReporter = function() {}; |
| 28 | + exports.StackdriverErrorReporter = StackdriverErrorReporter; |
24 | 29 |
|
25 | 30 | /** |
26 | | - * Initialize the StackdriverErrorReporting object. |
| 31 | + * Initialize the StackdriverErrorReporter object. |
27 | 32 | * @param {Object} config - the init configuration. |
28 | 33 | * @param {String} config.key - the API key to use to call the API. |
29 | 34 | * @param {String} config.projectId - the Google Cloud Platform project ID to report errors to. |
|
32 | 37 | * @param {Boolean} [config.reportUncaughtExceptions=true] - Set to false to stop reporting unhandled exceptions. |
33 | 38 | * @param {Boolean} [config.disabled=false] - Set to true to not report errors when calling report(), this can be used when developping locally. |
34 | 39 | */ |
35 | | - StackdriverErrorReporting.prototype.init = function(config) { |
| 40 | + StackdriverErrorReporter.prototype.start = function(config) { |
36 | 41 | if(!config.key) { |
37 | 42 | throw new Error('Cannot initialize: No API key provided.'); |
38 | 43 | } |
|
42 | 47 |
|
43 | 48 | this.apiKey = config.key; |
44 | 49 | this.projectId = config.projectId; |
45 | | - this.serviceContext = {service: 'web'}; |
46 | | - if(config.service) { |
47 | | - this.serviceContext.service = config.service; |
48 | | - } |
| 50 | + this.serviceContext = {service: config.service || 'web'}; |
49 | 51 | if(config.version) { |
50 | 52 | this.serviceContext.version = config.version; |
51 | 53 | } |
52 | | - this.reportUncaughtExceptions = config.reportUncaughtExceptions || true; |
| 54 | + this.reportUncaughtExceptions = !(config.reportUncaughtExceptions === false); |
53 | 55 | this.disabled = config.disabled || false; |
54 | 56 |
|
55 | 57 | // Register as global error handler if requested |
56 | 58 | var that = this; |
57 | 59 | if(this.reportUncaughtExceptions) { |
58 | | - var oldErrorHandler = function(){}; |
59 | | - if(window.onerror) { |
60 | | - oldErrorHandler = window.onerror; |
61 | | - } |
| 60 | + var oldErrorHandler = window.onerror || function(){}; |
62 | 61 |
|
63 | 62 | window.onerror = function(message, source, lineno, colno, error) { |
64 | 63 | if(error){ |
|
73 | 72 | /** |
74 | 73 | * Report an error to the Stackdriver Error Reporting API |
75 | 74 | * @param {Error|String} err - The Error object or message string to report. |
| 75 | + * @param callback - Calback function to be called once error has been reported. |
76 | 76 | */ |
77 | | - StackdriverErrorReporting.prototype.report = function(err, callback) { |
| 77 | + StackdriverErrorReporter.prototype.report = function(err, callback) { |
78 | 78 | if(this.disabled) { |
79 | 79 | return typeof callback === 'function' && callback(); |
80 | 80 | } |
|
103 | 103 | firstFrameIndex = 1; |
104 | 104 | } |
105 | 105 | var that = this; |
| 106 | + // This will use sourcemaps and normalize the stack frames |
106 | 107 | StackTrace.fromError(err).then(function(stack){ |
107 | 108 | payload.message = err.toString(); |
108 | 109 | for(var s = firstFrameIndex; s < stack.length; s++) { |
109 | 110 | payload.message += '\n'; |
110 | | - // reconstruct the stackframe to look like a JS stackframe. |
111 | | - // stack[s].source should not be used because not populated created from source map. |
| 111 | + // Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers. |
| 112 | + // stack[s].source should not be used because not populated when created from source map. |
112 | 113 | payload.message += [' at ', stack[s].getFunctionName(), ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join(''); |
113 | 114 | } |
114 | 115 | that.sendErrorPayload(payload, callback); |
115 | 116 | }); |
116 | 117 | }; |
117 | 118 |
|
118 | | - StackdriverErrorReporting.prototype.sendErrorPayload = function(payload, callback) { |
119 | | - var baseUrl = "https://clouderrorreporting.googleapis.com/v1beta1/projects/"; |
120 | | - var url = baseUrl + this.projectId + "/events:report?key=" + this.apiKey; |
| 119 | + StackdriverErrorReporter.prototype.sendErrorPayload = function(payload, callback) { |
| 120 | + var url = baseAPIUrl + this.projectId + "/events:report?key=" + this.apiKey; |
121 | 121 |
|
122 | 122 | var xhr = new XMLHttpRequest(); |
123 | 123 | xhr.open('POST', url, true); |
|
0 commit comments