Skip to content

Commit

Permalink
add createExpectedError method and fix xhr-impl
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinmombay committed Dec 19, 2016
1 parent f6d4aad commit a4a008e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class Log {
error(tag, var_args) {
const error = this.error_.apply(this, arguments);
if (error) {
this.win.setTimeout(() => {throw error;});
this.win.setTimeout(() => {throw /** @type {!Error} */ (error);});
}
}

Expand All @@ -216,7 +216,7 @@ export class Log {
const error = this.error_.apply(this, arguments);
if (error) {
error.expected = true;
this.win.setTimeout(() => {throw error;});
this.win.setTimeout(() => {throw /** @type {!Error} */ (error);});
}
}

Expand All @@ -231,6 +231,18 @@ export class Log {
return error;
}

/**
* Creates an error object with its expected property set to true.
* @param {...*} var_args
* @return {!Error}
*/
createExpectedError(var_args) {
const error = createErrorVargs.apply(null, arguments);
this.prepareError_(error);
error.expected = true;
return error;
}

/**
* Throws an error if the first argument isn't trueish.
*
Expand Down
2 changes: 1 addition & 1 deletion src/service/viewer-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class Viewer {
} else {
resolve(this.win.document.referrer);
if (this.unconfirmedReferrerUrl_ != this.win.document.referrer) {
dev().error(TAG_, 'Untrusted viewer referrer override: ' +
dev().expectedError(TAG_, 'Untrusted viewer referrer override: ' +
this.unconfirmedReferrerUrl_ + ' at ' +
this.messagingOrigin_);
this.unconfirmedReferrerUrl_ = this.win.document.referrer;
Expand Down
8 changes: 4 additions & 4 deletions src/service/xhr-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export function fetchPolyfill(input, opt_init) {
}
if (xhr.status < 100 || xhr.status > 599) {
xhr.onreadystatechange = null;
reject(new Error(`Unknown HTTP status ${xhr.status}`));
reject(user().createExpectedError(`Unknown HTTP status ${xhr.status}`));
return;
}

Expand All @@ -365,10 +365,10 @@ export function fetchPolyfill(input, opt_init) {
}
};
xhr.onerror = () => {
reject(new Error('Network failure'));
reject(user().createExpectedError('Network failure'));
};
xhr.onabort = () => {
reject(new Error('Request aborted'));
reject(user().createExpectedError('Request aborted'));
};

if (init.method == 'POST') {
Expand All @@ -394,7 +394,7 @@ function createXhrRequest(method, url) {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
throw new Error('CORS is not supported');
throw dev().createExpectedError('CORS is not supported');
}
return xhr;
}
Expand Down
7 changes: 7 additions & 0 deletions test/functional/test-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,15 @@ describe('Logging', () => {
expect(false).to.be.true;
});

it('should create expected error from message', () => {
const error = log.createExpectedError('test');
expect(error).to.be.instanceof(Error);
expect(error.expected).to.be.true;
});

it('should create suffixed errors from message', () => {
const error = log.createError('test');
expect(error.expected).to.be.undefined;
expect(error).to.be.instanceof(Error);
expect(isUserErrorMessage(error.message)).to.be.true;
expect(error.message).to.contain('test');
Expand Down

0 comments on commit a4a008e

Please sign in to comment.