Skip to content

Commit

Permalink
Make util.error() throw an Error #2021
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkfranz committed Nov 10, 2017
1 parent d3d252f commit 1f1b5be
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/collection/algorithms/bellman-ford.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var elesfn = ({
var weight = weightFn( edges[ e ] );

if( cost[ sourceIndex ] + weight < cost[ targetIndex ] ){
util.error( 'Graph contains a negative weight cycle for Bellman-Ford' );
util.warn( 'Graph contains a negative weight cycle for Bellman-Ford' );
return { pathTo: undefined,
distanceTo: undefined,
hasNegativeWeightCycle: true};
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let corefn = ({
let Layout = cy.extension( 'layout', name );

if( Layout == null ){
util.error( 'Can not apply layout: No such layout `' + name + '` found; did you include its JS file?' );
util.error( 'No such layout `' + name + '` found. Did you forget to import it and `cytoscape.use()` it?' );
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let corefn = ({

let RendererProto = cy.extension( 'renderer', options.name );
if( RendererProto == null ){
util.error( 'Can not initialise: No such renderer `%s` found; did you include its JS file?', options.name );
util.error( `Can not initialise: No such renderer \`${options.name}\` found. Did you forget to import it and \`cytoscape.use()\` it?` );
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/renderer/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BRp.init = function( options ){
var position = computedStyle.getPropertyValue('position');

if( position === 'static' ){
util.error('A Cytoscape container has style position:static and so can not use UI extensions properly');
util.warn('A Cytoscape container has style position:static and so can not use UI extensions properly');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/selector/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ let exprs = [
regex: tokens.subject,
populate: function( selector, query ){
if( selector.currentSubject != null && query.subject != query ){
util.error( 'Redefinition of subject in selector `' + selector.toString() + '`' );
util.warn( 'Redefinition of subject in selector `' + selector.toString() + '`' );
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/selector/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ let parse = function( selector ){
let check = consumeExpr( remaining );

if( check.expr == null ){
util.error( 'The selector `' + selector + '`is invalid' );
util.warn( 'The selector `' + selector + '`is invalid' );
return false;
} else {
let args = check.match.slice( 1 );
Expand Down Expand Up @@ -110,10 +110,10 @@ let parse = function( selector ){

query = ancestor; // go up the tree
} else if( query.source || query.target || query.connectedNodes ){
util.error( 'The selector `' + self.text() + '` can not contain a subject selector that applies to the source or target of an edge selector' );
util.warn( 'The selector `' + self.text() + '` can not contain a subject selector that applies to the source or target of an edge selector' );
return false;
} else {
util.error( 'When adjusting references for the selector `' + self.text() + '`, neither parent nor ancestor was found' );
util.warn( 'When adjusting references for the selector `' + self.text() + '`, neither parent nor ancestor was found' );
return false;
}
} // for
Expand Down
4 changes: 2 additions & 2 deletions src/style/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ styfn.applyParsedProperty = function( ele, parsedProp ){
}

let printMappingErr = function(){
util.error( 'Do not assign mappings to elements without corresponding data (e.g. ele `' + ele.id() + '` for property `' + prop.name + '` with data field `' + prop.field + '`); try a `[' + prop.field + ']` selector to limit scope to elements with `' + prop.field + '` defined' );
util.warn( 'Do not assign mappings to elements without corresponding data (e.g. ele `' + ele.id() + '` for property `' + prop.name + '` with data field `' + prop.field + '`); try a `[' + prop.field + ']` selector to limit scope to elements with `' + prop.field + '` defined' );
};

// put the property in the style objects
Expand All @@ -375,7 +375,7 @@ styfn.applyParsedProperty = function( ele, parsedProp ){

let percent;
if( !is.number( fieldVal ) ){ // then don't apply and fall back on the existing style
util.error('Do not use continuous mappers without specifying numeric data (i.e. `' + prop.field + ': ' + fieldVal + '` for `' + ele.id() + '` is non-numeric)');
util.warn('Do not use continuous mappers without specifying numeric data (i.e. `' + prop.field + ': ' + fieldVal + '` for `' + ele.id() + '` is non-numeric)');
return false;
} else {
percent = (fieldVal - prop.fieldMin) / (prop.fieldMax - prop.fieldMin);
Expand Down
6 changes: 3 additions & 3 deletions src/style/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ styfn.parseImplWarn = function( name, value, propIsBypass, propIsFlat ){
let prop = this.parseImpl( name, value, propIsBypass, propIsFlat );

if( !prop && value != null ){
util.error('The style property `%s: %s` is invalid', name, value);
util.warn(`The style property \`${name}: value\` is invalid`);
}

return prop;
Expand Down Expand Up @@ -104,7 +104,7 @@ styfn.parseImpl = function( name, value, propIsBypass, propIsFlat ){
if( !valueIsString || propIsFlat ){
// then don't bother to do the expensive regex checks

} else if( data = new RegExp( types.data.regex ).exec( value ) ){
} else if(( data = new RegExp( types.data.regex ).exec( value ) )){
if( propIsBypass ){ return false; } // mappers not allowed in bypass

let mapped = types.data;
Expand All @@ -118,7 +118,7 @@ styfn.parseImpl = function( name, value, propIsBypass, propIsFlat ){
bypass: propIsBypass
};

} else if( mapData = new RegExp( types.mapData.regex ).exec( value ) ){
} else if(( mapData = new RegExp( types.mapData.regex ).exec( value ) )){
if( propIsBypass ){ return false; } // mappers not allowed in bypass
if( type.multiple ){ return false; } // impossible to map to num

Expand Down
14 changes: 7 additions & 7 deletions src/style/string-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ styfn.appendFromString = function( string ){
}
}

while( true ){
for(;;){
let nothingLeftToParse = remaining.match( /^\s*$/ );
if( nothingLeftToParse ){ break; }

let selAndBlock = remaining.match( /^\s*((?:.|\s)+?)\s*\{((?:.|\s)+?)\}/ );

if( !selAndBlock ){
util.error( 'Halting stylesheet parsing: String stylesheet contains more to parse but no selector and block found in: ' + remaining );
util.warn( 'Halting stylesheet parsing: String stylesheet contains more to parse but no selector and block found in: ' + remaining );
break;
}

Expand All @@ -50,7 +50,7 @@ styfn.appendFromString = function( string ){
if( selectorStr !== 'core' ){
let selector = new Selector( selectorStr );
if( selector._private.invalid ){
util.error( 'Skipping parsing of block: Invalid selector found in string stylesheet: ' + selectorStr );
util.warn( 'Skipping parsing of block: Invalid selector found in string stylesheet: ' + selectorStr );

// skip this selector and block
removeSelAndBlockFromRemaining();
Expand All @@ -64,14 +64,14 @@ styfn.appendFromString = function( string ){
blockRem = blockStr;
let props = [];

while( true ){
for(;;){
let nothingLeftToParse = blockRem.match( /^\s*$/ );
if( nothingLeftToParse ){ break; }

let propAndVal = blockRem.match( /^\s*(.+?)\s*:\s*(.+?)\s*;/ );

if( !propAndVal ){
util.error( 'Skipping parsing of block: Invalid formatting of style property and value definitions found in:' + blockStr );
util.warn( 'Skipping parsing of block: Invalid formatting of style property and value definitions found in:' + blockStr );
invalidBlock = true;
break;
}
Expand All @@ -82,7 +82,7 @@ styfn.appendFromString = function( string ){

let prop = self.properties[ propStr ];
if( !prop ){
util.error( 'Skipping property: Invalid property name in: ' + propAndValStr );
util.warn( 'Skipping property: Invalid property name in: ' + propAndValStr );

// skip this property in the block
removePropAndValFromRem();
Expand All @@ -92,7 +92,7 @@ styfn.appendFromString = function( string ){
let parsedProp = style.parse( propStr, valStr );

if( !parsedProp ){
util.error( 'Skipping property: Invalid property definition in: ' + propAndValStr );
util.warn( 'Skipping property: Invalid property definition in: ' + propAndValStr );

// skip this property in the block
removePropAndValFromRem();
Expand Down
20 changes: 12 additions & 8 deletions src/util/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*global console */

let is = require( '../is' );
let warnSupported = console.warn != null; // eslint-disable-line no-console
let traceSupported = console.trace != null; // eslint-disable-line no-console

let util = {

Expand All @@ -15,18 +17,20 @@ let util = {
noop: function(){},

error: function( msg ){
/* eslint-disable */
if( console.error ){
console.error.apply( console, arguments );
throw new Error( msg );
},

if( console.trace ){ console.trace(); }
warn: function( msg ){ /* eslint-disable no-console */
if( warnSupported ){
console.warn( msg );
} else {
console.log.apply( console, arguments );
console.log( msg );

if( console.trace ){ console.trace(); }
if( traceSupported ){
console.trace();
}
}
/* eslint-enable */
},
}, /* eslint-enable */

clone: function( obj ){
return this.extend( {}, obj );
Expand Down
49 changes: 21 additions & 28 deletions test/core-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,33 @@ describe('Core initialisation', function(){
});
});

it('does not create an edge with bad source and target', function(done){
cytoscape({
headless: true,
it('does not create an edge with bad source and target', function(){
var init = function(){
cytoscape({
headless: true,

elements: {
edges: [ { data: { source: "n1", target: "n2" } } ]
},
ready: function(){
var cy = this;

expect( cy.elements().length ).to.equal(0);
elements: {
edges: [ { data: { source: "n1", target: "n2" } } ]
}
});
};

done();
}
});
expect(init).to.throw();
});

it('does not create an edge with bad target', function(done){
cytoscape({
headless: true,

elements: {
nodes: [ { data: { id: "n1" } } ],
edges: [ { data: { source: "n1", target: "n2" } } ]
},
ready: function(){
var cy = this;
it('does not create an edge with bad target', function(){
var init = function(){
cytoscape({
headless: true,

expect( cy.edges().size() ).to.equal( 0 );
expect( cy.nodes().size() ).to.equal( 1 );
elements: {
nodes: [ { data: { id: "n1" } } ],
edges: [ { data: { source: "n1", target: "n2" } } ]
}
});
};

done();
}
});
expect(init).to.throw();
});

it('creates an edge that specifies good source and target', function(done){
Expand Down

0 comments on commit 1f1b5be

Please sign in to comment.