|
40 | 40 | * @param {Boolean} [config.disabled=false] - Set to true to not report errors when calling report(), this can be used when developping locally. |
41 | 41 | */ |
42 | 42 | StackdriverErrorReporter.prototype.start = function(config) { |
43 | | - if(!config.key && !config.targetUrl) { |
| 43 | + if (!config.key && !config.targetUrl) { |
44 | 44 | throw new Error('Cannot initialize: No API key or target url provided.'); |
45 | 45 | } |
46 | | - if(!config.projectId && !config.targetUrl) { |
| 46 | + if (!config.projectId && !config.targetUrl) { |
47 | 47 | throw new Error('Cannot initialize: No project ID or target url provided.'); |
48 | 48 | } |
49 | | - if(typeof StackTrace === 'undefined') { |
| 49 | + if (typeof StackTrace === 'undefined') { |
50 | 50 | // Inform about missing dependency |
51 | 51 | throw new Error('make sure you loaded “dist/stackdriver-errors-concat.js” or “dist/stackdriver-errors-concat.min.js”, or that you imported the “stacktrace-js” module'); |
52 | 52 | } |
|
56 | 56 | this.targetUrl = config.targetUrl; |
57 | 57 | this.context = config.context || {}; |
58 | 58 | this.serviceContext = {service: config.service || 'web'}; |
59 | | - if(config.version) { |
| 59 | + if (config.version) { |
60 | 60 | this.serviceContext.version = config.version; |
61 | 61 | } |
62 | 62 | this.reportUncaughtExceptions = config.reportUncaughtExceptions !== false; |
|
66 | 66 | // Register as global error handler if requested |
67 | 67 | var noop = function() {}; |
68 | 68 | var that = this; |
69 | | - if(this.reportUncaughtExceptions) { |
| 69 | + if (this.reportUncaughtExceptions) { |
70 | 70 | var oldErrorHandler = window.onerror || noop; |
71 | 71 |
|
72 | 72 | window.onerror = function(message, source, lineno, colno, error) { |
73 | | - if(error){ |
| 73 | + if (error) { |
74 | 74 | that.report(error).catch(noop); |
75 | 75 | } |
76 | 76 | oldErrorHandler(message, source, lineno, colno, error); |
77 | 77 | return true; |
78 | 78 | }; |
79 | 79 | } |
80 | | - if(this.reportUnhandledPromiseRejections) { |
| 80 | + if (this.reportUnhandledPromiseRejections) { |
81 | 81 | var oldPromiseRejectionHandler = window.onunhandledrejection || noop; |
82 | 82 |
|
83 | 83 | window.onunhandledrejection = function(promiseRejectionEvent) { |
84 | | - if(promiseRejectionEvent){ |
| 84 | + if (promiseRejectionEvent) { |
85 | 85 | that.report(promiseRejectionEvent.reason).catch(noop); |
86 | 86 | } |
87 | 87 | oldPromiseRejectionHandler(promiseRejectionEvent.reason); |
|
96 | 96 | * @returns {Promise} A promise that completes when the report has been sent. |
97 | 97 | */ |
98 | 98 | StackdriverErrorReporter.prototype.report = function(err) { |
99 | | - if(this.disabled) { |
| 99 | + if (this.disabled) { |
100 | 100 | return Promise.resolve(null); |
101 | 101 | } |
102 | | - if(!err) { |
| 102 | + if (!err) { |
103 | 103 | return Promise.reject(new Error('no error to report')); |
104 | 104 | } |
105 | 105 |
|
|
108 | 108 | payload.context = this.context; |
109 | 109 | payload.context.httpRequest = { |
110 | 110 | userAgent: window.navigator.userAgent, |
111 | | - url: window.location.href |
| 111 | + url: window.location.href, |
112 | 112 | }; |
113 | 113 |
|
114 | 114 | var firstFrameIndex = 0; |
115 | | - if(typeof err == 'string' || err instanceof String) { |
| 115 | + if (typeof err == 'string' || err instanceof String) { |
116 | 116 | // Transform the message in an error, use try/catch to make sure the stacktrace is populated. |
117 | 117 | try { |
118 | 118 | throw new Error(err); |
119 | | - } catch(e) { |
| 119 | + } catch (e) { |
120 | 120 | err = e; |
121 | 121 | } |
122 | 122 | // the first frame when using report() is always this library |
|
125 | 125 | var that = this; |
126 | 126 | // This will use sourcemaps and normalize the stack frames |
127 | 127 | // eslint-disable-next-line no-undef |
128 | | - return StackTrace.fromError(err).then(function(stack){ |
| 128 | + return StackTrace.fromError(err).then(function(stack) { |
129 | 129 | payload.message = err.toString(); |
130 | | - for(var s = firstFrameIndex; s < stack.length; s++) { |
| 130 | + for (var s = firstFrameIndex; s < stack.length; s++) { |
131 | 131 | payload.message += '\n'; |
132 | 132 | // Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers. |
133 | 133 | // stack[s].source should not be used because not populated when created from source map. |
134 | | - // |
| 134 | + // |
135 | 135 | // If functionName or methodName isn't available <anonymous> will be used as the name. |
136 | | - payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join(''); |
| 136 | + payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber(), ':', stack[s].getColumnNumber(), ')'].join(''); |
137 | 137 | } |
138 | 138 | return that.sendErrorPayload(payload); |
139 | 139 | }, function(reason) { |
|
0 commit comments