Skip to content

Commit

Permalink
Redirect assert statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Voytenko committed Mar 21, 2016
1 parent dd68263 commit a641c3b
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 82 deletions.
4 changes: 2 additions & 2 deletions 3p/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {loadScript} from '../src/3p';
import {assert} from '../src/asserts';
import {user} from '../src/log';


/**
Expand All @@ -40,7 +40,7 @@ function getFacebookSdk(global, cb) {
*/
export function facebook(global, data) {
const embedAs = data.embedAs || 'post';
assert(['post', 'video'].indexOf(embedAs) !== -1,
user.assert(['post', 'video'].indexOf(embedAs) !== -1,
'Attribute data-embed-as for <amp-facebook> value is wrong, should be' +
' "post" or "video" was: %s', embedAs);
const fbPost = document.createElement('div');
Expand Down
6 changes: 3 additions & 3 deletions builtins/amp-ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import {BaseElement} from '../src/base-element';
import {IntersectionObserver} from '../src/intersection-observer';
import {assert} from '../src/asserts';
import {cidForOrNull} from '../src/cid';
import {getIframe, prefetchBootstrap} from '../src/3p-frame';
import {isLayoutSizeDefined} from '../src/layout';
Expand All @@ -26,8 +25,9 @@ import {parseUrl} from '../src/url';
import {registerElement} from '../src/custom-element';
import {adPrefetch, adPreconnect, clientIdScope} from '../ads/_config';
import {timer} from '../src/timer';
import {viewerFor} from '../src/viewer';
import {user} from '../src/log';
import {userNotificationManagerFor} from '../src/user-notification';
import {viewerFor} from '../src/viewer';


/** @private @const These tags are allowed to have fixed positioning */
Expand Down Expand Up @@ -223,7 +223,7 @@ export function installAd(win) {
layoutCallback() {
if (!this.iframe_) {
loadingAdsCount++;
assert(!this.isInFixedContainer_,
user.assert(!this.isInFixedContainer_,
'<amp-ad> is not allowed to be placed in elements with ' +
'position:fixed: %s', this.element);
timer.delay(() => {
Expand Down
4 changes: 2 additions & 2 deletions builtins/amp-pixel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import {BaseElement} from '../src/base-element';
import {Layout} from '../src/layout';
import {urlReplacementsFor} from '../src/url-replacements';
import {assert} from '../src/asserts';
import {registerElement} from '../src/custom-element';
import {toggle} from '../src/style';
import {user} from '../src/log';


/**
Expand Down Expand Up @@ -62,7 +62,7 @@ export function installPixel(win) {
}

assertSource(src) {
assert(
user.assert(
/^(https\:\/\/|\/\/)/i.test(src),
'The <amp-pixel> src attribute must start with ' +
'"https://" or "//". Invalid value: ' + src);
Expand Down
11 changes: 6 additions & 5 deletions src/3p-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/


import {assert} from './asserts';
import {getLengthNumeral} from '../src/layout';
import {getService} from './service';
import {documentInfoFor} from './document-info';
import {getMode} from './mode';
import {preconnectFor} from './preconnect';
import {dashToCamelCase} from './string';
import {parseUrl, assertHttpsUrl} from './url';
import {user} from './log';
import {viewerFor} from './viewer';


Expand All @@ -45,7 +45,7 @@ function getFrameAttributes(parentWindow, element, opt_type) {
const width = element.getAttribute('width');
const height = element.getAttribute('height');
const type = opt_type || element.getAttribute('type');
assert(type, 'Attribute type required for <amp-ad>: %s', element);
user.assert(type, 'Attribute type required for <amp-ad>: %s', element);
const attributes = {};
// Do these first, as the other attributes have precedence.
addDataAndJsonAttributes_(element, attributes);
Expand Down Expand Up @@ -139,7 +139,8 @@ export function addDataAndJsonAttributes_(element, attributes) {
try {
obj = JSON.parse(json);
} catch (e) {
assert(false, 'Error parsing JSON in json attribute in element %s',
throw user.createError(
'Error parsing JSON in json attribute in element %s',
element);
}
for (const key in obj) {
Expand Down Expand Up @@ -231,14 +232,14 @@ function getCustomBootstrapBaseUrl(parentWindow, opt_strictForUnitTest) {
return null;
}
const url = assertHttpsUrl(meta.getAttribute('content'), meta);
assert(url.indexOf('?') == -1,
user.assert(url.indexOf('?') == -1,
'3p iframe url must not include query string %s in element %s.',
url, meta);
// This is not a security primitive, we just don't want this to happen in
// practice. People could still redirect to the same origin, but they cannot
// redirect to the proxy origin which is the important one.
const parsed = parseUrl(url);
assert((parsed.hostname == 'localhost' && !opt_strictForUnitTest) ||
user.assert((parsed.hostname == 'localhost' && !opt_strictForUnitTest) ||
parsed.origin != parseUrl(parentWindow.location.href).origin,
'3p iframe url must not be on the same origin as the current document ' +
'%s (%s) in element %s. See https://github.com/ampproject/amphtml/blob/' +
Expand Down
12 changes: 6 additions & 6 deletions src/3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Note: loaded by 3p system. Cannot rely on babel polyfills.


import {assert} from './asserts';
import {dev, user} from './log';
import {isArray} from './types';
import {rethrowAsync} from './log';

Expand All @@ -45,7 +45,7 @@ let syncScriptLoads = 0;
* @param {ThirdPartyFunctionDef} draw Function that draws the 3p integration.
*/
export function register(id, draw) {
assert(!registrations[id], 'Double registration %s', id);
dev.assert(!registrations[id], 'Double registration %s', id);
registrations[id] = draw;
}

Expand All @@ -57,7 +57,7 @@ export function register(id, draw) {
*/
export function run(id, win, data) {
const fn = registrations[id];
assert(fn, 'Unknown 3p: ' + id);
user.assert(fn, 'Unknown 3p: ' + id);
fn(win, data);
}

Expand Down Expand Up @@ -200,7 +200,7 @@ export function computeInMasterFrame(global, taskId, work, cb) {
export function validateDataExists(data, mandatoryFields) {
for (let i = 0; i < mandatoryFields.length; i++) {
const field = mandatoryFields[i];
assert(data[field],
user.assert(data[field],
'Missing attribute for %s: %s.', data.type, field);
}
}
Expand All @@ -221,7 +221,7 @@ export function validateExactlyOne(data, alternativeFields) {
}
}

assert(countFileds === 1,
user.assert(countFileds === 1,
'%s must contain exactly one of attributes: %s.',
data.type,
alternativeFields.join(', '));
Expand Down Expand Up @@ -252,7 +252,7 @@ export function validateData(data, allowedFields) {
field in defaultAvailableFields) {
continue;
}
assert(allowedFields.indexOf(field) != -1,
user.assert(allowedFields.indexOf(field) != -1,
'Unknown attribute for %s: %s.', data.type, field);
}
}
37 changes: 19 additions & 18 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import {Layout, getLayoutClass, getLengthNumeral, getLengthUnits,
parseLayout, parseLength, getNaturalDimensions,
hasNaturalDimensions} from './layout';
import {ElementStub, stubbedElements} from './element-stub';
import {assert} from './asserts';
import {createLoaderElement} from '../src/loader';
import {dev, rethrowAsync, user} from './log';
import {getIntersectionChangeEntry} from '../src/intersection-observer';
import {parseSizeList} from './size-list';
import {reportError} from './error';
import {resourcesFor} from './resources';
import {rethrowAsync, user} from './log';
import {timer} from './timer';
import {vsyncFor} from './vsync';
import {getServicePromise, getServicePromiseOrNull} from './service';
Expand Down Expand Up @@ -78,7 +77,7 @@ export function upgradeOrRegisterElement(win, name, toClass) {
registerElement(win, name, toClass);
return;
}
assert(knownElements[name] == ElementStub,
user.assert(knownElements[name] == ElementStub,
'%s is already registered. The script tag for ' +
'%s is likely included twice in the page.', name, name);
for (let i = 0; i < stubbedElements.length; i++) {
Expand Down Expand Up @@ -140,12 +139,13 @@ export function applyLayout_(element) {

// Input layout attributes.
const inputLayout = layoutAttr ? parseLayout(layoutAttr) : null;
assert(inputLayout !== undefined, 'Unknown layout: %s', layoutAttr);
user.assert(inputLayout !== undefined, 'Unknown layout: %s', layoutAttr);
const inputWidth = (widthAttr && widthAttr != 'auto') ?
parseLength(widthAttr) : widthAttr;
assert(inputWidth !== undefined, 'Invalid width value: %s', widthAttr);
user.assert(inputWidth !== undefined, 'Invalid width value: %s', widthAttr);
const inputHeight = heightAttr ? parseLength(heightAttr) : null;
assert(inputHeight !== undefined, 'Invalid height value: %s', heightAttr);
user.assert(inputHeight !== undefined, 'Invalid height value: %s',
heightAttr);

// Effective layout attributes. These are effectively constants.
let width;
Expand Down Expand Up @@ -183,24 +183,24 @@ export function applyLayout_(element) {
// Verify layout attributes.
if (layout == Layout.FIXED || layout == Layout.FIXED_HEIGHT ||
layout == Layout.RESPONSIVE) {
assert(height, 'Expected height to be available: %s', heightAttr);
user.assert(height, 'Expected height to be available: %s', heightAttr);
}
if (layout == Layout.FIXED_HEIGHT) {
assert(!width || width == 'auto',
user.assert(!width || width == 'auto',
'Expected width to be either absent or equal "auto" ' +
'for fixed-height layout: %s', widthAttr);
}
if (layout == Layout.FIXED || layout == Layout.RESPONSIVE) {
assert(width && width != 'auto',
user.assert(width && width != 'auto',
'Expected width to be available and not equal to "auto": %s',
widthAttr);
}
if (layout == Layout.RESPONSIVE) {
assert(getLengthUnits(width) == getLengthUnits(height),
user.assert(getLengthUnits(width) == getLengthUnits(height),
'Length units should be the same for width and height: %s, %s',
widthAttr, heightAttr);
} else {
assert(heightsAttr === null,
user.assert(heightsAttr === null,
'Unexpected "heights" attribute for none-responsive layout');
}

Expand Down Expand Up @@ -362,7 +362,7 @@ export function createAmpElementProto(win, name, implementationClass) {

/** @private */
ElementProto.assertNotTemplate_ = function() {
assert(!this.isInTemplate_, 'Must never be called in template');
dev.assert(!this.isInTemplate_, 'Must never be called in template');
};

/**
Expand Down Expand Up @@ -437,7 +437,7 @@ export function createAmpElementProto(win, name, implementationClass) {
if (this.isBuilt()) {
return true;
}
assert(this.isUpgraded(), 'Cannot build unupgraded element');
dev.assert(this.isUpgraded(), 'Cannot build unupgraded element');
if (!force && !this.implementation_.isReadyToBuild()) {
return false;
}
Expand Down Expand Up @@ -738,7 +738,7 @@ export function createAmpElementProto(win, name, implementationClass) {
*/
ElementProto.layoutCallback = function() {
this.assertNotTemplate_();
assert(this.isUpgraded() && this.isBuilt(),
dev.assert(this.isUpgraded() && this.isBuilt(),
'Must be upgraded and built to receive viewport events');
this.dispatchCustomEvent('amp:load:start');
const promise = this.implementation_.layoutCallback();
Expand Down Expand Up @@ -866,7 +866,7 @@ export function createAmpElementProto(win, name, implementationClass) {
ElementProto.enqueAction = function(invocation) {
this.assertNotTemplate_();
if (!this.isBuilt()) {
assert(this.actionQueue_).push(invocation);
dev.assert(this.actionQueue_).push(invocation);
} else {
this.executionAction_(invocation, false);
}
Expand All @@ -882,7 +882,7 @@ export function createAmpElementProto(win, name, implementationClass) {
return;
}

const actionQueue = assert(this.actionQueue_);
const actionQueue = dev.assert(this.actionQueue_);
this.actionQueue_ = null;

// TODO(dvoytenko, #1260): dedupe actions.
Expand Down Expand Up @@ -1168,7 +1168,8 @@ export function registerElement(win, name, implementationClass) {
* @return {boolean} Whether this element is scheduled to be loaded.
*/
function isElementScheduled(win, elementName) {
assert(win.ampExtendedElements, 'win.ampExtendedElements not created yet');
dev.assert(win.ampExtendedElements,
'win.ampExtendedElements not created yet');
return !!win.ampExtendedElements[elementName];
}

Expand Down Expand Up @@ -1236,7 +1237,7 @@ export function resetScheduledElementForTesting(win, elementName) {
export function getElementService(win, id, providedByElement) {
return getElementServiceIfAvailable(win, id, providedByElement).then(
service => {
return assert(service,
return user.assert(service,
'Service %s was requested to be provided through %s, ' +
'but %s is not loaded in the current page. To fix this ' +
'problem load the JavaScript file for %s in this page.',
Expand Down
4 changes: 2 additions & 2 deletions src/document-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import {getService} from './service';
import {assert} from './asserts';
import {parseUrl, getSourceUrl} from './url';
import {user} from './log';

/**
* @param {!Window} win
Expand All @@ -29,7 +29,7 @@ import {parseUrl, getSourceUrl} from './url';
export function documentInfoFor(win) {
return getService(win, 'documentInfo', () => {
return {
canonicalUrl: parseUrl(assert(
canonicalUrl: parseUrl(user.assert(
win.document.querySelector('link[rel=canonical]'),
'AMP files are required to have a <link rel=canonical> tag.')
.href).href,
Expand Down
12 changes: 4 additions & 8 deletions src/event-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {timer} from './timer';
import {assert} from './asserts';
import {user} from './log';


/**
Expand Down Expand Up @@ -112,13 +112,9 @@ export function loadPromise(element, opt_timeout) {
unlistenLoad = listenOnce(element, 'load', () => resolve(element));
}
unlistenError = listenOnce(element, 'error', () => {
try {
// Report failed loads as asserts so that they automatically go into
// the "document error" bucket.
assert(false, 'Failed HTTP request for %s.', element);
} catch (e) {
reject(e);
}
// Report failed loads as asserts so that they automatically go into
// the "document error" bucket.
reject(user.createError('Failed HTTP request for %s.', element));
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import {Observable} from './observable';
import {assert} from './asserts';
import {Pass} from './pass';
import {dev} from './log';
import {timer} from './timer';

const PROP_ = '__AMP_Gestures';
Expand Down Expand Up @@ -350,7 +350,7 @@ export class Gestures {
* @private
*/
signalEmit_(recognizer, data, event) {
assert(this.eventing_ == recognizer,
dev.assert(this.eventing_ == recognizer,
'Recognizer is not currently allowed: %s', recognizer.getType());
const overserver = this.overservers_[recognizer.getType()];
if (overserver) {
Expand Down
4 changes: 2 additions & 2 deletions src/iframe-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {assert} from './asserts';
import {dev} from './log';
import {parseUrl} from './url';


Expand All @@ -30,7 +30,7 @@ import {parseUrl} from './url';
* @return {!Unlisten}
*/
export function listen(iframe, typeOfMessage, callback, opt_is3P) {
assert(iframe.src, 'only iframes with src supported');
dev.assert(iframe.src, 'only iframes with src supported');
const origin = parseUrl(iframe.src).origin;
let win = iframe.ownerDocument.defaultView;
const sentinel = getSentinel_(opt_is3P);
Expand Down
Loading

0 comments on commit a641c3b

Please sign in to comment.