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 dfaf29a commit d1f3715
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 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
3 changes: 2 additions & 1 deletion src/service/storage-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ export class LocalStorageBinding {
this.isLocalStorageSupported_ = 'localStorage' in this.win;

if (!this.isLocalStorageSupported_) {
dev().expectedError(TAG, 'localStorage not supported.');
const error = new Error('localStorage not supported.');
dev().expectedError(TAG, error);
}
}

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
3 changes: 1 addition & 2 deletions test/functional/test-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ describe('LocalStorageBinding', () => {
});

it('should throw if localStorage is not supported', () => {
const errorSpy = sandbox.spy(dev(), 'error');
const errorSpy = sandbox.spy(dev(), 'expectedError');

expect(errorSpy.callCount).to.equal(0);
new LocalStorageBinding(windowApi);
Expand All @@ -456,7 +456,6 @@ describe('LocalStorageBinding', () => {
new LocalStorageBinding(windowApi);
expect(errorSpy.callCount).to.equal(1);
expect(errorSpy.args[0][1].message).to.match(/localStorage not supported/);
expect(errorSpy.args[0][1].expected).to.be.true;
});

it('should load store when available', () => {
Expand Down
6 changes: 4 additions & 2 deletions test/functional/test-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Viewer', () => {
let clock;
let events;
let errorStub;
let expectedErrorStub;

function changeVisibility(vis) {
windowApi.document.hidden = vis !== 'visible';
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('Viewer', () => {
installTimerService(windowApi);
events = {};
errorStub = sandbox.stub(dev(), 'error');
expectedErrorStub = sandbox.stub(dev(), 'expectedError');
windowMock = sandbox.mock(windowApi);
viewer = new Viewer(ampdoc);
});
Expand Down Expand Up @@ -1051,8 +1053,8 @@ describe('Viewer', () => {
// Unconfirmed referrer is reset. Async error is thrown.
expect(viewer.getUnconfirmedReferrerUrl())
.to.equal('https://acme.org/docref');
expect(errorStub.callCount).to.equal(1);
expect(errorStub.calledWith('Viewer',
expect(expectedErrorStub.callCount).to.equal(1);
expect(expectedErrorStub.calledWith('Viewer',
sinon.match(arg => {
return !!arg.match(/Untrusted viewer referrer override/);
}))).to.be.true;
Expand Down

0 comments on commit d1f3715

Please sign in to comment.