-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
error.js
721 lines (659 loc) · 19.8 KB
/
error.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {AmpEvents} from './amp-events';
import {Services} from './services';
import {
USER_ERROR_SENTINEL,
dev,
duplicateErrorIfNecessary,
isUserErrorEmbed,
isUserErrorMessage,
} from './log';
import {dict} from './utils/object';
import {experimentTogglesOrNull, getBinaryType, isCanary} from './experiments';
import {exponentialBackoff} from './exponential-backoff';
import {getMode} from './mode';
import {isLoadErrorMessage} from './event-helper';
import {isProxyOrigin} from './url';
import {makeBodyVisibleRecovery} from './style-installer';
import {startsWith} from './string';
import {triggerAnalyticsEvent} from './analytics';
import {urls} from './config';
/**
* @const {string}
*/
const CANCELLED = 'CANCELLED';
/**
* @const {string}
*/
const BLOCK_BY_CONSENT = 'BLOCK_BY_CONSENT';
/**
* @const {string}
*/
const ABORTED = 'AbortError';
/**
* The threshold for errors throttled because nothing can be done about
* them, but we'd still like to report the rough number.
* @const {number}
*/
const NON_ACTIONABLE_ERROR_THROTTLE_THRESHOLD = 0.001;
/**
* The threshold for errors throttled because nothing can be done about
* them, but we'd still like to report the rough number.
* @const {number}
*/
const USER_ERROR_THROTTLE_THRESHOLD = 0.1;
/**
* Collects error messages, so they can be included in subsequent reports.
* That allows identifying errors that might be caused by previous errors.
*/
let accumulatedErrorMessages = self.__AMP_ERRORS || [];
// Use a true global, to avoid multi-module inclusion issues.
self.__AMP_ERRORS = accumulatedErrorMessages;
/**
* Pushes element into array, keeping at most the most recent limit elements
*
* @param {!Array<T>} array
* @param {T} element
* @param {number} limit
* @template T
*/
function pushLimit(array, element, limit) {
if (array.length >= limit) {
array.splice(0, array.length - limit + 1);
}
array.push(element);
}
/**
* A wrapper around our exponentialBackoff, to lazy initialize it to avoid an
* un-DCE'able side-effect.
* @param {function()} work the function to execute after backoff
* @return {number} the setTimeout id
*/
let reportingBackoff = function(work) {
// Set reportingBackoff as the lazy-created function. JS Vooodoooo.
reportingBackoff = exponentialBackoff(1.5);
return reportingBackoff(work);
};
/**
* Attempts to stringify a value, falling back to String.
* @param {*} value
* @return {string}
*/
function tryJsonStringify(value) {
try {
// Cast is fine, because we really don't care here. Just trying.
return JSON.stringify(/** @type {!JsonObject} */ (value));
} catch (e) {
return String(value);
}
}
/**
* The true JS engine, as detected by inspecting an Error stack. This should be
* used with the userAgent to tell definitely. I.e., Chrome on iOS is really a
* Safari JS engine.
*/
let detectedJsEngine;
/**
* @param {!Window} win
* @param {*} error
* @param {!Element=} opt_associatedElement
*/
export function reportErrorForWin(win, error, opt_associatedElement) {
reportError(error, opt_associatedElement);
if (
error &&
!!win &&
isUserErrorMessage(error.message) &&
!isUserErrorEmbed(error.message)
) {
reportErrorToAnalytics(/** @type {!Error} */ (error), win);
}
}
/**
* Reports an error. If the error has an "associatedElement" property
* the element is marked with the `i-amphtml-element-error` and displays
* the message itself. The message is always send to the console.
* If the error has a "messageArray" property, that array is logged.
* This way one gets the native fidelity of the console for things like
* elements instead of stringification.
* @param {*} error
* @param {!Element=} opt_associatedElement
* @return {!Error}
*/
export function reportError(error, opt_associatedElement) {
try {
// Convert error to the expected type.
let isValidError;
if (error) {
if (error.message !== undefined) {
error = duplicateErrorIfNecessary(/** @type {!Error} */ (error));
isValidError = true;
} else {
const origError = error;
error = new Error(tryJsonStringify(origError));
error.origError = origError;
}
} else {
error = new Error('Unknown error');
}
// Report if error is not an expected type.
if (!isValidError && getMode().localDev && !getMode().test) {
setTimeout(function() {
const rethrow = new Error(
'_reported_ Error reported incorrectly: ' + error
);
throw rethrow;
});
}
if (error.reported) {
return /** @type {!Error} */ (error);
}
error.reported = true;
// Update element.
const element = opt_associatedElement || error.associatedElement;
if (element && element.classList) {
element.classList.add('i-amphtml-error');
if (getMode().development) {
element.classList.add('i-amphtml-element-error');
element.setAttribute('error-message', error.message);
}
}
// Report to console.
if (self.console) {
const output = console.error || console.log;
if (error.messageArray) {
output.apply(console, error.messageArray);
} else {
if (element) {
output.call(console, error.message, element);
} else if (!getMode().minified) {
output.call(console, error.stack);
} else {
output.call(console, error.message);
}
}
}
if (element && element.dispatchCustomEventForTesting) {
element.dispatchCustomEventForTesting(AmpEvents.ERROR, error.message);
}
// 'call' to make linter happy. And .call to make compiler happy
// that expects some @this.
onError['call'](
undefined,
undefined,
undefined,
undefined,
undefined,
error
);
} catch (errorReportingError) {
setTimeout(function() {
throw errorReportingError;
});
}
return /** @type {!Error} */ (error);
}
/**
* Returns an error for a cancellation of a promise.
* @return {!Error}
*/
export function cancellation() {
return new Error(CANCELLED);
}
/**
* @param {*} errorOrMessage
* @return {boolean}
*/
export function isCancellation(errorOrMessage) {
if (!errorOrMessage) {
return false;
}
if (typeof errorOrMessage == 'string') {
return startsWith(errorOrMessage, CANCELLED);
}
if (typeof errorOrMessage.message == 'string') {
return startsWith(errorOrMessage.message, CANCELLED);
}
return false;
}
/**
* Returns an error for component blocked by consent
* @return {!Error}
*/
export function blockedByConsentError() {
return new Error(BLOCK_BY_CONSENT);
}
/**
* @param {*} errorOrMessage
* @return {boolean}
*/
export function isBlockedByConsent(errorOrMessage) {
if (!errorOrMessage) {
return false;
}
if (typeof errorOrMessage == 'string') {
return startsWith(errorOrMessage, BLOCK_BY_CONSENT);
}
if (typeof errorOrMessage.message == 'string') {
return startsWith(errorOrMessage.message, BLOCK_BY_CONSENT);
}
return false;
}
/**
* Install handling of global unhandled exceptions.
* @param {!Window} win
*/
export function installErrorReporting(win) {
win.onerror = /** @type {!Function} */ (onError);
win.addEventListener('unhandledrejection', event => {
if (
event.reason &&
(event.reason.message === CANCELLED ||
event.reason.message === BLOCK_BY_CONSENT ||
event.reason.message === ABORTED)
) {
event.preventDefault();
return;
}
reportError(event.reason || new Error('rejected promise ' + event));
});
}
/**
* Signature designed, so it can work with window.onerror
* @param {string|undefined} message
* @param {string|undefined} filename
* @param {string|undefined} line
* @param {string|undefined} col
* @param {*|undefined} error
* @this {!Window|undefined}
*/
function onError(message, filename, line, col, error) {
// Make an attempt to unhide the body.
if (this && this.document) {
makeBodyVisibleRecovery(this.document);
}
if (getMode().localDev || getMode().development || getMode().test) {
return;
}
let hasNonAmpJs = false;
try {
hasNonAmpJs = detectNonAmpJs(self);
} catch (ignore) {
// Ignore errors during error report generation.
}
if (hasNonAmpJs && Math.random() > 0.01) {
// Only report 1% of errors on pages with non-AMP JS.
// These errors can almost never be acted upon, but spikes such as
// due to buggy browser extensions may be helpful to notify authors.
return;
}
const data = getErrorReportData(
message,
filename,
line,
col,
error,
hasNonAmpJs
);
if (data) {
reportingBackoff(() => {
try {
return reportErrorToServerOrViewer(
this,
/** @type {!JsonObject} */ (data)
).catch(() => {
// catch async errors to avoid recursive errors.
});
} catch (e) {
// catch async errors to avoid recursive errors.
}
});
}
}
/**
* Passes the given error data to either server or viewer.
* @param {!Window} win
* @param {!JsonObject} data Data from `getErrorReportData`.
* @return {Promise<undefined>}
*/
export function reportErrorToServerOrViewer(win, data) {
// Report the error to viewer if it has the capability. The data passed
// to the viewer is exactly the same as the data passed to the server
// below.
return maybeReportErrorToViewer(win, data).then(reportedErrorToViewer => {
if (!reportedErrorToViewer) {
const xhr = new XMLHttpRequest();
xhr.open('POST', urls.errorReporting, true);
xhr.send(JSON.stringify(data));
}
});
}
/**
* Passes the given error data to the viewer if the following criteria is met:
* - The viewer is a trusted viewer
* - The viewer has the `errorReporter` capability
* - The AMP doc is in single doc mode
* - The AMP doc is opted-in for error interception (`<html>` tag has the
* `report-errors-to-viewer` attribute)
*
* @param {!Window} win
* @param {!JsonObject} data Data from `getErrorReportData`.
* @return {!Promise<boolean>} `Promise<True>` if the error was sent to the
* viewer, `Promise<False>` otherwise.
* @visibleForTesting
*/
export function maybeReportErrorToViewer(win, data) {
const ampdocService = Services.ampdocServiceFor(win);
if (!ampdocService.isSingleDoc()) {
return Promise.resolve(false);
}
const ampdocSingle = ampdocService.getSingleDoc();
const htmlElement = ampdocSingle.getRootNode().documentElement;
const docOptedIn = htmlElement.hasAttribute('report-errors-to-viewer');
if (!docOptedIn) {
return Promise.resolve(false);
}
const viewer = Services.viewerForDoc(ampdocSingle);
if (!viewer.hasCapability('errorReporter')) {
return Promise.resolve(false);
}
return viewer.isTrustedViewer().then(viewerTrusted => {
if (!viewerTrusted) {
return false;
}
viewer.sendMessage('error', errorReportingDataForViewer(data));
return true;
});
}
/**
* Strips down the error reporting data to a minimal set
* to be sent to the viewer.
* @param {!JsonObject} errorReportData
* @return {!JsonObject}
* @visibleForTesting
*/
export function errorReportingDataForViewer(errorReportData) {
return dict({
'm': errorReportData['m'], // message
'a': errorReportData['a'], // isUserError
's': errorReportData['s'], // error stack
'el': errorReportData['el'], // tagName
'ex': errorReportData['ex'], // expected error?
'v': errorReportData['v'], // runtime
'jse': errorReportData['jse'], // detectedJsEngine
});
}
/**
* @param {string|undefined} message
* @param {*|undefined} error
* @return {string}
*/
function buildErrorMessage_(message, error) {
if (error) {
if (error.message) {
message = error.message;
} else {
// This should never be a string, but sometimes it is.
message = String(error);
}
}
if (!message) {
message = 'Unknown error';
}
return message;
}
/**
* Signature designed, so it can work with window.onerror
* @param {string|undefined} message
* @param {string|undefined} filename
* @param {string|undefined} line
* @param {string|undefined} col
* @param {*|undefined} error
* @param {boolean} hasNonAmpJs
* @return {!JsonObject|undefined} The data to post
* visibleForTesting
*/
export function getErrorReportData(
message,
filename,
line,
col,
error,
hasNonAmpJs
) {
message = buildErrorMessage_(message, error);
// An "expected" error is still an error, i.e. some features are disabled
// or not functioning fully because of it. However, it's an expected
// error. E.g. as is the case with some browser API missing (storage).
// Thus, the error can be classified differently by log aggregators.
// The main goal is to monitor that an "expected" error doesn't deteriorate
// over time. It's impossible to completely eliminate it.
let expected = !!(error && error.expected);
if (/_reported_/.test(message)) {
return;
}
if (message == CANCELLED) {
return;
}
const detachedWindow = !(self && self.window);
const throttleBase = Math.random();
// We throttle load errors and generic "Script error." errors
// that have no information and thus cannot be acted upon.
if (
isLoadErrorMessage(message) ||
// See https://github.com/ampproject/amphtml/issues/7353
// for context.
message == 'Script error.' ||
// Window has become detached, really anything can happen
// at this point.
detachedWindow
) {
expected = true;
if (throttleBase > NON_ACTIONABLE_ERROR_THROTTLE_THRESHOLD) {
return;
}
}
const isUserError = isUserErrorMessage(message);
// Only report a subset of user errors.
if (isUserError && throttleBase > USER_ERROR_THROTTLE_THRESHOLD) {
return;
}
// This is the App Engine app in
// https://github.com/ampproject/error-tracker
// It stores error reports via https://cloud.google.com/error-reporting/
// for analyzing production issues.
const data = /** @type {!JsonObject} */ (Object.create(null));
data['v'] = getMode().rtvVersion;
data['noAmp'] = hasNonAmpJs ? '1' : '0';
data['m'] = message.replace(USER_ERROR_SENTINEL, '');
data['a'] = isUserError ? '1' : '0';
// Errors are tagged with "ex" ("expected") label to allow loggers to
// classify these errors as benchmarks and not exceptions.
data['ex'] = expected ? '1' : '0';
data['dw'] = detachedWindow ? '1' : '0';
let runtime = '1p';
if (self.context && self.context.location) {
data['3p'] = '1';
runtime = '3p';
} else if (getMode().runtime) {
runtime = getMode().runtime;
}
if (getMode().singlePassType) {
data['spt'] = getMode().singlePassType;
}
data['rt'] = runtime;
// Add our a4a id if we are inabox
if (runtime === 'inabox') {
data['adid'] = getMode().a4aId;
}
// TODO(erwinm): Remove ca when all systems read `bt` instead of `ca` to
// identify js binary type.
data['ca'] = isCanary(self) ? '1' : '0';
// Pass binary type.
data['bt'] = getBinaryType(self);
if (self.location.ancestorOrigins && self.location.ancestorOrigins[0]) {
data['or'] = self.location.ancestorOrigins[0];
}
if (self.viewerState) {
data['vs'] = self.viewerState;
}
// Is embedded?
if (self.parent && self.parent != self) {
data['iem'] = '1';
}
if (self.AMP && self.AMP.viewer) {
const resolvedViewerUrl = self.AMP.viewer.getResolvedViewerUrl();
const messagingOrigin = self.AMP.viewer.maybeGetMessagingOrigin();
if (resolvedViewerUrl) {
data['rvu'] = resolvedViewerUrl;
}
if (messagingOrigin) {
data['mso'] = messagingOrigin;
}
}
if (!detectedJsEngine) {
detectedJsEngine = detectJsEngineFromStack();
}
data['jse'] = detectedJsEngine;
const exps = [];
const experiments = experimentTogglesOrNull(self);
for (const exp in experiments) {
const on = experiments[exp];
exps.push(`${exp}=${on ? '1' : '0'}`);
}
data['exps'] = exps.join(',');
if (error) {
const tagName = error.associatedElement
? error.associatedElement.tagName
: 'u'; // Unknown
data['el'] = tagName;
if (error.args) {
data['args'] = JSON.stringify(error.args);
}
if (!isUserError && !error.ignoreStack && error.stack) {
data['s'] = error.stack;
}
// TODO(jridgewell, #18574); Make sure error is always an object.
if (error.message) {
error.message += ' _reported_';
}
} else {
data['f'] = filename || '';
data['l'] = line || '';
data['c'] = col || '';
}
data['r'] = self.document ? self.document.referrer : '';
data['ae'] = accumulatedErrorMessages.join(',');
data['fr'] = self.location.originalHash || self.location.hash;
pushLimit(accumulatedErrorMessages, message, 25);
return data;
}
/**
* Returns true if it appears like there is non-AMP JS on the
* current page.
* @param {!Window} win
* @return {boolean}
* @visibleForTesting
*/
export function detectNonAmpJs(win) {
if (!win.document) {
return false;
}
const scripts = win.document.querySelectorAll('script[src]');
for (let i = 0; i < scripts.length; i++) {
if (!isProxyOrigin(scripts[i].src.toLowerCase())) {
return true;
}
}
return false;
}
/**
* Resets accumulated error messages for testing
*/
export function resetAccumulatedErrorMessagesForTesting() {
accumulatedErrorMessages = [];
}
/**
* Does a series of checks on the stack of an thrown error to determine the
* JS engine that is currently running. This gives a bit more information than
* just the UserAgent, since browsers often allow overriding it to "emulate"
* mobile.
* @return {string}
* @visibleForTesting
*/
export function detectJsEngineFromStack() {
/** @constructor */
function Fn() {}
Fn.prototype.t = function() {
throw new Error('message');
};
const object = new Fn();
try {
object.t();
} catch (e) {
const {stack} = e;
// Safari only mentions the method name.
if (startsWith(stack, 't@')) {
return 'Safari';
}
// Firefox mentions "prototype".
if (stack.indexOf('.prototype.t@') > -1) {
return 'Firefox';
}
// IE looks like Chrome, but includes a context for the base stack line.
// Explicitly, we're looking for something like:
// " at Global code (https://example.com/app.js:1:200)" or
// " at Anonymous function (https://example.com/app.js:1:200)"
// vs Chrome which has:
// " at https://example.com/app.js:1:200"
const last = stack.split('\n').pop();
if (/\bat .* \(/i.test(last)) {
return 'IE';
}
// Finally, chrome includes the error message in the stack.
if (startsWith(stack, 'Error: message')) {
return 'Chrome';
}
}
return 'unknown';
}
/**
* @param {!Error} error
* @param {!Window} win
*/
export function reportErrorToAnalytics(error, win) {
// Currently this can only be executed in a single-doc mode. Otherwise,
// it's not clear which ampdoc the event would belong too.
if (Services.ampdocServiceFor(win).isSingleDoc()) {
const vars = dict({
'errorName': error.name,
'errorMessage': error.message,
});
triggerAnalyticsEvent(getRootElement_(win), 'user-error', vars);
}
}
/**
* @param {!Window} win
* @return {!Element}
* @private
*/
function getRootElement_(win) {
const root = Services.ampdocServiceFor(win)
.getSingleDoc()
.getRootNode();
return dev().assertElement(root.documentElement || root.body || root);
}