Permalink
Browse files

Merge pull request #1664 from rachel-fenichel/cleanup/throw_errors

Throw Errors instead of using goog.asserts
  • Loading branch information...
rachel-fenichel committed Aug 16, 2018
2 parents 731ab04 + c88b1f8 commit 698af3fcbb5e18a0e32cc2ead7713b77e20c809b
View
@@ -40,11 +40,11 @@ goog.require('Blockly.FieldLabelSerializable');
goog.require('Blockly.FieldVariableGetter');
goog.require('Blockly.Input');
goog.require('Blockly.Mutator');
goog.require('Blockly.utils');
goog.require('Blockly.Warning');
goog.require('Blockly.Workspace');
goog.require('Blockly.Xml');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
goog.require('goog.string');
@@ -174,15 +174,16 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
/** @type {string} */
this.type = prototypeName;
var prototype = Blockly.Blocks[prototypeName];
goog.asserts.assertObject(prototype,
'Error: Unknown block type "%s".', prototypeName);
if (!prototype || typeof prototype != 'object') {
throw TypeError('Unknown block type: ' + prototypeName);
}
goog.mixin(this, prototype);
}
workspace.addTopBlock(this);
// Call an initialization function, if it exists.
if (goog.isFunction(this.init)) {
if (typeof this.init == 'function') {
this.init();
}
// Record initial inline state.
@@ -205,7 +206,7 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
}
// Bind an onchange function, if it exists.
if (goog.isFunction(this.onchange)) {
if (typeof this.onchange == 'function') {
this.setOnChange(this.onchange);
}
};
@@ -546,14 +547,14 @@ Blockly.Block.prototype.setParent = function(newParent) {
}
if (this.parentBlock_) {
// Remove this block from the old parent's child list.
goog.array.remove(this.parentBlock_.childBlocks_, this);
Blockly.utils.arrayRemove(this.parentBlock_.childBlocks_, this);
// Disconnect from superior blocks.
if (this.previousConnection && this.previousConnection.isConnected()) {
throw 'Still connected to previous block.';
throw Error('Still connected to previous block.');
}
if (this.outputConnection && this.outputConnection.isConnected()) {
throw 'Still connected to parent block.';
throw Error('Still connected to parent block.');
}
this.parentBlock_ = null;
// This block hasn't actually moved on-screen, so there's no need to update
@@ -841,8 +842,8 @@ Blockly.Block.prototype.setColour = function(colour, colourSecondary, colourTert
* @throws {Error} if onchangeFn is not falsey or a function.
*/
Blockly.Block.prototype.setOnChange = function(onchangeFn) {
if (onchangeFn && !goog.isFunction(onchangeFn)) {
throw new Error("onchange must be a function.");
if (onchangeFn && typeof onchangeFn != 'function') {
throw new Error('onchange must be a function.');
}
if (this.onchangeWrapper_) {
this.workspace.removeChangeListener(this.onchangeWrapper_);
@@ -964,7 +965,9 @@ Blockly.Block.prototype.getFieldValue = function(name) {
*/
Blockly.Block.prototype.setFieldValue = function(newValue, name) {
var field = this.getField(name);
goog.asserts.assertObject(field, 'Field "%s" not found.', name);
if (!field) {
throw Error('Field "' + name + '" not found.');
}
field.setValue(newValue);
};
@@ -980,16 +983,20 @@ Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) {
opt_check = null;
}
if (!this.previousConnection) {
goog.asserts.assert(!this.outputConnection,
'Remove output connection prior to adding previous connection.');
if (this.outputConnection) {
throw Error('Remove output connection prior to adding previous ' +
'connection.');
}
this.previousConnection =
this.makeConnection_(Blockly.PREVIOUS_STATEMENT);
}
this.previousConnection.setCheck(opt_check);
} else {
if (this.previousConnection) {
goog.asserts.assert(!this.previousConnection.isConnected(),
'Must disconnect previous statement before removing connection.');
if (this.previousConnection.isConnected()) {
throw Error('Must disconnect previous statement before removing ' +
'connection.');
}
this.previousConnection.dispose();
this.previousConnection = null;
}
@@ -1013,8 +1020,10 @@ Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) {
this.nextConnection.setCheck(opt_check);
} else {
if (this.nextConnection) {
goog.asserts.assert(!this.nextConnection.isConnected(),
'Must disconnect next statement before removing connection.');
if (this.nextConnection.isConnected()) {
throw Error('Must disconnect next statement before removing ' +
'connection.');
}
this.nextConnection.dispose();
this.nextConnection = null;
}
@@ -1034,15 +1043,18 @@ Blockly.Block.prototype.setOutput = function(newBoolean, opt_check) {
opt_check = null;
}
if (!this.outputConnection) {
goog.asserts.assert(!this.previousConnection,
'Remove previous connection prior to adding output connection.');
if (this.previousConnection) {
throw Error('Remove previous connection prior to adding output ' +
'connection.');
}
this.outputConnection = this.makeConnection_(Blockly.OUTPUT_VALUE);
}
this.outputConnection.setCheck(opt_check);
} else {
if (this.outputConnection) {
goog.asserts.assert(!this.outputConnection.isConnected(),
'Must disconnect output value before removing connection.');
if (this.outputConnection.isConnected()) {
throw Error('Must disconnect output value before removing connection.');
}
this.outputConnection.dispose();
this.outputConnection = null;
}
@@ -1168,12 +1180,14 @@ Blockly.Block.prototype.toString = function(opt_maxLength, opt_emptyToken) {
}
}
}
text = goog.string.trim(text.join(' ')) || '???';
text = text.join(' ').trim() || '???';
if (opt_maxLength) {
// TODO: Improve truncation so that text from this block is given priority.
// E.g. "1+2+3+4+5+6+7+8+9=0" should be "...6+7+8+9=0", not "1+2+3+4+5...".
// E.g. "1+2+3+4+5=6+7+8+9+0" should be "...4+5=6+7...".
text = goog.string.truncate(text, opt_maxLength);
if (text.length > opt_maxLength) {
text = text.substring(0, opt_maxLength - 3) + '...';
}
}
return text;
};
@@ -1217,9 +1231,10 @@ Blockly.Block.prototype.jsonInit = function(json) {
var warningPrefix = json['type'] ? 'Block "' + json['type'] + '": ' : '';
// Validate inputs.
goog.asserts.assert(
json['output'] == undefined || json['previousStatement'] == undefined,
warningPrefix + 'Must not have both an output and a previousStatement.');
if (json['output'] && json['previousStatement']) {
throw Error(warningPrefix +
'Must not have both an output and a previousStatement.');
}
// Set basic properties of block.
if (json['colour'] !== undefined) {
@@ -1261,9 +1276,11 @@ Blockly.Block.prototype.jsonInit = function(json) {
var localizedValue = Blockly.utils.replaceMessageReferences(rawValue);
this.setHelpUrl(localizedValue);
}
if (goog.isString(json['extensions'])) {
console.warn('JSON attribute \'extensions\' should be an array of ' +
'strings. Found raw string in JSON for \'' + json['type'] + '\' block.');
if (typeof json['extensions'] == 'string') {
console.warn(
warningPrefix + 'JSON attribute \'extensions\' should be an array of' +
' strings. Found raw string in JSON for \'' + json['type'] +
'\' block.');
json['extensions'] = [json['extensions']]; // Correct and continue.
}
@@ -1300,8 +1317,8 @@ Blockly.Block.prototype.jsonInit = function(json) {
* @param {boolean=} opt_disableCheck Option flag to disable overwrite checks.
*/
Blockly.Block.prototype.mixin = function(mixinObj, opt_disableCheck) {
if (goog.isDef(opt_disableCheck) && !goog.isBoolean(opt_disableCheck)) {
throw new Error("opt_disableCheck must be a boolean if provided");
if (opt_disableCheck !== undefined && typeof opt_disableCheck != 'boolean') {
throw new Error('opt_disableCheck must be a boolean if provided');
}
if (!opt_disableCheck) {
var overwrites = [];
@@ -1393,7 +1410,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
}
// Add last dummy input if needed.
if (elements.length && (typeof elements[elements.length - 1] == 'string' ||
goog.string.startsWith(
Blockly.utils.startsWith(
elements[elements.length - 1]['type'], 'field_'))) {
var dummyInput = {type: 'input_dummy'};
if (lastDummyAlign) {
@@ -1513,9 +1530,12 @@ Blockly.Block.prototype.moveInputBefore = function(name, refName) {
}
}
}
goog.asserts.assert(inputIndex != -1, 'Named input "%s" not found.', name);
goog.asserts.assert(
refIndex != -1, 'Reference input "%s" not found.', refName);
if (inputIndex == -1) {
throw Error('Named input "' + name + '" not found.');
}
if (refIndex == -1) {
throw Error('Reference input "' + refName + '" not found.');
}
this.moveNumberedInputBefore(inputIndex, refIndex);
};
@@ -1527,11 +1547,15 @@ Blockly.Block.prototype.moveInputBefore = function(name, refName) {
Blockly.Block.prototype.moveNumberedInputBefore = function(
inputIndex, refIndex) {
// Validate arguments.
goog.asserts.assert(inputIndex != refIndex, 'Can\'t move input to itself.');
goog.asserts.assert(inputIndex < this.inputList.length,
'Input index ' + inputIndex + ' out of bounds.');
goog.asserts.assert(refIndex <= this.inputList.length,
'Reference input ' + refIndex + ' out of bounds.');
if (inputIndex == refIndex) {
throw Error('Can\'t move input to itself.');
}
if (inputIndex >= this.inputList.length) {
throw RangeError('Input index ' + inputIndex + ' out of bounds.');
}
if (refIndex > this.inputList.length) {
throw RangeError('Reference input ' + refIndex + ' out of bounds.');
}
// Remove input.
var input = this.inputList[inputIndex];
this.inputList.splice(inputIndex, 1);
@@ -1546,7 +1570,7 @@ Blockly.Block.prototype.moveNumberedInputBefore = function(
* Remove an input from this block.
* @param {string} name The name of the input.
* @param {boolean=} opt_quiet True to prevent error if input is not present.
* @throws {goog.asserts.AssertionError} if the input is not present and
* @throws {Error} if the input is not present and
* opt_quiet is not true.
*/
Blockly.Block.prototype.removeInput = function(name, opt_quiet) {
@@ -1569,7 +1593,7 @@ Blockly.Block.prototype.removeInput = function(name, opt_quiet) {
}
}
if (!opt_quiet) {
goog.asserts.fail('Input "%s" not found.', name);
throw Error('Input not found: ' + name);
}
};
@@ -1702,7 +1726,9 @@ Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
* @param {number} dy Vertical offset, in workspace units.
*/
Blockly.Block.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
if (this.parentBlock_) {
throw Error('Block has parent.');
}
var event = new Blockly.Events.BlockMove(this);
this.xy_.translate(dx, dy);
event.recordNew();
View
@@ -31,7 +31,6 @@
goog.provide('Blockly.BlockDragSurfaceSvg');
goog.require('Blockly.utils');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
@@ -179,8 +178,9 @@ Blockly.BlockDragSurfaceSvg.prototype.createDropShadowDom_ = function(defs) {
* surface.
*/
Blockly.BlockDragSurfaceSvg.prototype.setBlocksAndShow = function(blocks) {
goog.asserts.assert(
this.dragGroup_.childNodes.length == 0, 'Already dragging a block.');
if (this.dragGroup_.childNodes.length) {
throw Error('Already dragging a block.');
}
// appendChild removes the blocks from the previous parent
this.dragGroup_.appendChild(blocks);
this.SVG_.style.display = 'block';
@@ -286,8 +286,9 @@ Blockly.BlockDragSurfaceSvg.prototype.clearAndHide = function(opt_newSurface) {
this.dragGroup_.removeChild(this.getCurrentBlock());
}
this.SVG_.style.display = 'none';
goog.asserts.assert(
this.dragGroup_.childNodes.length == 0, 'Drag group was not cleared.');
if (this.dragGroup_.childNodes.length) {
throw Error('Drag group was not cleared.');
}
this.surfaceXY_ = null;
// Reset the overflow property back to hidden so that nothing appears outside
View
@@ -33,7 +33,6 @@ goog.require('Blockly.Events.EndBlockDrag');
goog.require('Blockly.InsertionMarkerManager');
goog.require('goog.math.Coordinate');
goog.require('goog.asserts');
/**
View
@@ -300,7 +300,7 @@ Blockly.Events.Delete = function(block) {
return; // Blank event to be populated by fromJson.
}
if (block.getParent()) {
throw 'Connected blocks cannot be deleted.';
throw Error('Connected blocks cannot be deleted.');
}
Blockly.Events.Delete.superClass_.constructor.call(this, block);
View
@@ -39,7 +39,6 @@ goog.require('Blockly.Touch');
goog.require('Blockly.utils');
goog.require('goog.Timer');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.math.Coordinate');
@@ -144,7 +143,9 @@ Blockly.BlockSvg.INLINE = -1;
* May be called more than once.
*/
Blockly.BlockSvg.prototype.initSvg = function() {
goog.asserts.assert(this.workspace.rendered, 'Workspace is headless.');
if (!this.workspace.rendered) {
throw TypeError('Workspace is headless.');
}
if (!this.isInsertionMarker()) { // Insertion markers not allowed to have inputs or icons
// Input shapes are empty holes drawn when a value input is not connected.
for (var i = 0, input; input = this.inputList[i]; i++) {
@@ -362,7 +363,9 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
* @param {number} dy Vertical offset in workspace units.
*/
Blockly.BlockSvg.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
if (this.parentBlock_) {
throw Error('Block has parent.');
}
var eventsEnabled = Blockly.Events.isEnabled();
if (eventsEnabled) {
var event = new Blockly.Events.BlockMove(this);
@@ -648,7 +651,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) {
* @private
*/
Blockly.BlockSvg.prototype.showHelp_ = function() {
var url = goog.isFunction(this.helpUrl) ? this.helpUrl() : this.helpUrl;
var url = (typeof this.helpUrl == 'function') ? this.helpUrl() : this.helpUrl;
if (url) {
// @todo rewrite
alert(url);
@@ -984,7 +987,7 @@ Blockly.BlockSvg.prototype.getCommentText = function() {
Blockly.BlockSvg.prototype.setCommentText = function(text, commentId,
commentX, commentY, minimized) {
var changedState = false;
if (goog.isString(text)) {
if (typeof text == 'string') {
if (!this.comment) {
this.comment = new Blockly.ScratchBlockComment(this, text, commentId,
commentX, commentY, minimized);
@@ -1000,7 +1003,7 @@ Blockly.BlockSvg.prototype.setCommentText = function(text, commentId,
}
if (changedState && this.rendered) {
this.render();
if (goog.isString(text)) {
if (typeof text == 'string') {
this.comment.setVisible(true);
}
// Adding or removing a comment icon will cause the block to change shape.
@@ -1049,7 +1052,7 @@ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) {
}
var changedState = false;
if (goog.isString(text)) {
if (typeof text == 'string') {
if (!this.warning) {
this.warning = new Blockly.Warning(this);
changedState = true;
View
@@ -31,7 +31,6 @@ goog.require('Blockly.Events.CommentMove');
goog.require('Blockly.WorkspaceCommentSvg');
goog.require('goog.math.Coordinate');
goog.require('goog.asserts');
/**
Oops, something went wrong.

0 comments on commit 698af3f

Please sign in to comment.