Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude errors generated from A4A iframe #10562

Merged
merged 25 commits into from
Aug 7, 2017
Merged

Conversation

tiendt
Copy link
Contributor

@tiendt tiendt commented Jul 20, 2017

User error Reporting:
Filter out errors generated from A4A iframe embed

@tiendt tiendt requested a review from lannka July 20, 2017 22:15
@tiendt
Copy link
Contributor Author

tiendt commented Jul 20, 2017

This is just initial skeleton, I'm hoping to see whether the if else logic in user() is good. The next thing to do is to get ampdoc here (right now there's no win object) @lannka

src/log.js Outdated
* @return {!Log}
*/
export function user() {
export function user(opt_element) {
if (logs.user) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should now cache 2 instances of user logger.

logs.user
logs.userForEmbed

logic will be like:

if (isFromEmbed(opt_element) {
  if (!logs.userForEmbed) {
     logs.userForEmbed = ...
  }
  return logs.userForEmbed;
} else {
  if (!logs.user) {
    logs.user = ...
  }
  return logs.user;
}

src/log.js Outdated
/**
* @return {boolean} Whether this message was a iframe embed error.
*/
function isErrorEmbed(message) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isUserErrorEmbed

src/log.js Outdated
*
* @const {string}
*/
export const EMBED = '\u200B\u200B\u200B\u200B';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USER_ERROR_EMBED_SENTINEL

src/log.js Outdated
if (logs.userForEmbed) {
return logs.userForEmbed;
}
logs.userForEmbed = new logConstructor(self, mode => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems duplicated logic, can we consolidate with getUserLogger?

src/error.js Outdated
if (error && isUserErrorMessage(error.message) && !!win) {
reportErrorToAnalytics(/** @type {!Error} */(error), win);
if (error && !!win) {
if (isUserErrorMessage(error.message) && !error.embed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems no nested if needed

});

it('should return logger for user-error', () => {
expect(user().suffix_).to.equal(USER_ERROR_SENTINEL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use

const error = user().createError();
expect(isUserErrorEmbed(error.message)).to.be.false;

also add test to make sure you not not creating extra identical logger

expect(user()).to.equal(user(element1));
expect(user()).to.equal(user(element2));

@@ -18,8 +18,8 @@
# Historically, amp-ad/amp-embed javascript were embedded into the main
# runtime (v0.js), so there are many pages that do not include the amp-ad
# extension. We want to warn for these.
# AMP-EMBED requires the amp-ad extension, not amp-embed.
# Including the amp-ad extension, but not an AMP-AD/AMP-EMBED tag should only
# AMP-USER_ERROR_EMBED_SENTINEL requires the amp-ad extension, not amp-embed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<amp-embed> stays <amp-embed>, please revert

src/error.js Outdated
@@ -97,7 +97,7 @@ let detectedJsEngine;
*/
export function reportErrorForWin(win, error, opt_associatedElement) {
reportError(error, opt_associatedElement);
if (error && isUserErrorMessage(error.message) && !!win) {
if (error && !!win && isUserErrorMessage(error.message) && !error.embed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use isUserErrorEmbed(error.message) here? Same reason to why we don't have error.isUserError.

src/log.js Outdated
@@ -210,6 +229,7 @@ export class Log {
const error = this.error_.apply(this, arguments);
if (error) {
error.name = tag || error.name;
error.embed = isUserErrorEmbed(error.message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought only error.message will survive, should we still use error.embed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i just realized, seems like error.embed is not needed anymore

@@ -341,6 +341,7 @@ var forbiddenTerms = {
'extensions/amp-access/0.1/amp-access.js',
'extensions/amp-experiment/0.1/variant.js',
'extensions/amp-user-notification/0.1/amp-user-notification.js',
'dist.3p/current/integration.js',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhouyx When i run presubmit it shows that i'm using cid, so Hongfei suggested to whitelist it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry if there's mis-understanding, but I don't see it cid used in your change. did you try make a new build and run presubmit again?

Copy link
Contributor

@lannka lannka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, some minor comments

src/log.js Outdated
* @return {boolean} Whether this message was a user error.
*/
export function isUserErrorMessage(message) {
return message.indexOf(USER_ERROR_SENTINEL) >= 0;
}

/**
* @return {boolean} Whether this message was a iframe embed error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether this message was a a user error from an iframe embed

src/log.js Outdated
* @return {!Log}
*/
export function user() {
export function user(opt_element) {
let logger;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems this local var is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lannka i think it is because I'd need to assign different value to this var and also use it at the last return call. Lmk what you think!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you just assign to logs.user ?

it('should return logger for user-error', () => {
const error = user().createError();
expect(isUserErrorEmbed(error.message)).to.be.false;
expect(user(this.element).suffix_).to.equal(USER_ERROR_SENTINEL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid checking member field

expect(isUserError(error.message)).to.be.true;

it('should return logger for embed-error', () => {
element = document.createElement('embed');
iframe.contentWindow.document.body.appendChild(element);
expect(user(element).suffix_).to.equal(USER_ERROR_EMBED_SENTINEL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

expect(user(element).suffix_).to.equal(USER_ERROR_EMBED_SENTINEL);
});

it('should not create extra identical logs', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loggers

iframe.contentWindow.document.body.appendChild(element1);
iframe.contentWindow.document.body.appendChild(element2);
expect(user()).to.equal(user(this.element));
expect(user(element1)).to.equal(user(element2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add one more check:

expect(user()).to.not.equal(user(element1));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lannka i made all requested change. Thanks!

src/log.js Outdated
if (logs.user) {
logs.user = logs.user;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

Copy link
Contributor

@zhouyx zhouyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one question.

user().error('AMP-IMG', 'Setting role=img on amp-img elements breaks ' +
'screen readers please just set alt or ARIA attributes, they will ' +
'be correctly propagated for the underlying <img> element.');
user(this.element).error('AMP-IMG',
Copy link
Contributor

@zhouyx zhouyx Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we plan to migrate to the new user(opt_element) in this PR? If so why we only make the change to amp-img.js. I would suggest migrate to new format in separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhouyx I need that change to amp-img.js to do the test, but the whole migration to use(opt_element) would be in a separate PR. Is that ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked offline. Please revert. and I'll approve and merge the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhouyx reverted!

src/log.js Outdated
*/
export function isFromEmbed(win, opt_element) {
if (!!opt_element) {
return opt_element.ownerDocument.defaultView != win;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: logically cleaner to flip the if

@lannka
Copy link
Contributor

lannka commented Aug 7, 2017

LGTM @zhouyx pls help merge once you have done the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants