chore(project): consistent async error handling

Async utilities properly propagate _all_ errors to the
specified _done_ callback. Failing in _done_ does not
re-invoke it.

Adjustments made in

* Viewer#importXML
* Viewer#saveSVG
* Importer#importBpmnDiagram

Tests cleaned up to remove un-necessary `if (err) { ...` snippets.
This commit is contained in:
Nico Rehwaldt 2017-12-04 22:27:32 +01:00
parent 105f326dad
commit 7ffb865023
9 changed files with 58 additions and 148 deletions

View File

@ -227,27 +227,33 @@ Viewer.prototype.saveSVG = function(options, done) {
options = {}; options = {};
} }
var canvas = this.get('canvas'); var svg, err;
var contentNode = canvas.getDefaultLayer(), try {
defsNode = domQuery('defs', canvas._svg); var canvas = this.get('canvas');
var contents = innerSVG(contentNode), var contentNode = canvas.getDefaultLayer(),
defs = defsNode ? '<defs>' + innerSVG(defsNode) + '</defs>' : ''; defsNode = domQuery('defs', canvas._svg);
var bbox = contentNode.getBBox(); var contents = innerSVG(contentNode),
defs = defsNode ? '<defs>' + innerSVG(defsNode) + '</defs>' : '';
var svg = var bbox = contentNode.getBBox();
'<?xml version="1.0" encoding="utf-8"?>\n' +
'<!-- created with bpmn-js / http://bpmn.io -->\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' +
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' +
'width="' + bbox.width + '" height="' + bbox.height + '" ' +
'viewBox="' + bbox.x + ' ' + bbox.y + ' ' + bbox.width + ' ' + bbox.height + '" version="1.1">' +
defs + contents +
'</svg>';
done(null, svg); svg =
'<?xml version="1.0" encoding="utf-8"?>\n' +
'<!-- created with bpmn-js / http://bpmn.io -->\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' +
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' +
'width="' + bbox.width + '" height="' + bbox.height + '" ' +
'viewBox="' + bbox.x + ' ' + bbox.y + ' ' + bbox.width + ' ' + bbox.height + '" version="1.1">' +
defs + contents +
'</svg>';
} catch (e) {
err = e;
}
done(err, svg);
}; };
/** /**
@ -292,12 +298,8 @@ Viewer.prototype.saveSVG = function(options, done) {
Viewer.prototype.importDefinitions = function(definitions, done) { Viewer.prototype.importDefinitions = function(definitions, done) {
var err; // catch synchronous exceptions during #clear()
// use try/catch to not swallow synchronous exceptions
// that may be raised during model parsing
try { try {
if (this._definitions) { if (this._definitions) {
// clear existing rendered diagram // clear existing rendered diagram
this.clear(); this.clear();
@ -305,15 +307,12 @@ Viewer.prototype.importDefinitions = function(definitions, done) {
// update definitions // update definitions
this._definitions = definitions; this._definitions = definitions;
// perform graphical import
return importBpmnDiagram(this, definitions, done);
} catch (e) { } catch (e) {
err = e; return done(e);
} }
// handle synchronous errors // perform graphical import
return done(err); return importBpmnDiagram(this, definitions, done);
}; };
Viewer.prototype.getModules = function() { Viewer.prototype.getModules = function() {

View File

@ -14,9 +14,9 @@ var BpmnTreeWalker = require('./BpmnTreeWalker');
*/ */
function importBpmnDiagram(diagram, definitions, done) { function importBpmnDiagram(diagram, definitions, done) {
var importer = diagram.get('bpmnImporter'), var importer,
eventBus = diagram.get('eventBus'), eventBus,
translate = diagram.get('translate'); translate;
var error, var error,
warnings = []; warnings = [];
@ -51,19 +51,23 @@ function importBpmnDiagram(diagram, definitions, done) {
walker.handleDefinitions(definitions); walker.handleDefinitions(definitions);
} }
eventBus.fire('import.render.start', { definitions: definitions });
try { try {
importer = diagram.get('bpmnImporter');
eventBus = diagram.get('eventBus');
translate = diagram.get('translate');
eventBus.fire('import.render.start', { definitions: definitions });
render(definitions); render(definitions);
eventBus.fire('import.render.complete', {
error: error,
warnings: warnings
});
} catch (e) { } catch (e) {
error = e; error = e;
} }
eventBus.fire('import.render.complete', {
error: error,
warnings: warnings
});
done(error, warnings); done(error, warnings);
} }

View File

@ -72,18 +72,10 @@ describe('Modeler', function() {
// given // given
createModeler(xml, function(err, warnings, modeler) { createModeler(xml, function(err, warnings, modeler) {
if (err) {
return done(err);
}
// when // when
// mimic re-import of same diagram // mimic re-import of same diagram
modeler.importXML(xml, function(err, warnings) { modeler.importXML(xml, function(err, warnings) {
if (err) {
return done(err);
}
// then // then
expect(warnings.length).to.equal(0); expect(warnings.length).to.equal(0);

View File

@ -54,18 +54,10 @@ describe('Viewer', function() {
// given // given
createViewer(xml, function(err, warnings, viewer) { createViewer(xml, function(err, warnings, viewer) {
if (err) {
return done(err);
}
// when // when
// mimic re-import of same diagram // mimic re-import of same diagram
viewer.importXML(xml, function(err, warnings) { viewer.importXML(xml, function(err, warnings) {
if (err) {
return done(err);
}
// then // then
expect(warnings.length).to.equal(0); expect(warnings.length).to.equal(0);
@ -343,17 +335,9 @@ describe('Viewer', function() {
createViewer(xml, function(err, warnings, viewer) { createViewer(xml, function(err, warnings, viewer) {
if (err) {
return done(err);
}
// when // when
viewer.saveXML({ format: true }, function(err, xml) { viewer.saveXML({ format: true }, function(err, xml) {
if (err) {
return done(err);
}
// then // then
expect(xml).to.contain('<?xml version="1.0" encoding="UTF-8"?>'); expect(xml).to.contain('<?xml version="1.0" encoding="UTF-8"?>');
expect(xml).to.contain('<bpmn2:definitions'); expect(xml).to.contain('<bpmn2:definitions');
@ -380,14 +364,10 @@ describe('Viewer', function() {
// when // when
viewer.saveSVG(function(err, svg) { viewer.saveSVG(function(err, svg) {
if (err) {
return done(err);
}
// then // then
expect(validSVG(svg)).to.be.true; expect(validSVG(svg)).to.be.true;
done(); done(err);
}); });
}); });
}); });
@ -411,17 +391,13 @@ describe('Viewer', function() {
// when // when
viewer.saveSVG(function(err, svg) { viewer.saveSVG(function(err, svg) {
if (err) {
return done(err);
}
// then // then
expect(validSVG(svg)).to.be.true; expect(validSVG(svg)).to.be.true;
// no svg export should not take too long // no svg export should not take too long
expect(currentTime() - time).to.be.below(1000); expect(currentTime() - time).to.be.below(1000);
done(); done(err);
}); });
}); });
}); });
@ -459,10 +435,6 @@ describe('Viewer', function() {
// when // when
viewer.saveSVG(function(err, svg) { viewer.saveSVG(function(err, svg) {
if (err) {
return done(err);
}
var svgDoc = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); var svgDoc = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svgDoc.innerHTML = svg; svgDoc.innerHTML = svg;
@ -470,7 +442,7 @@ describe('Viewer', function() {
expect(validSVG(svg)).to.be.true; expect(validSVG(svg)).to.be.true;
expect(svgDoc.querySelector('.outer-bound-marker')).to.be.null; expect(svgDoc.querySelector('.outer-bound-marker')).to.be.null;
done(); done(err);
}); });
}); });
}); });

View File

@ -180,10 +180,6 @@ describe('draw - bpmn renderer', function() {
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
inject(function(elementRegistry) { inject(function(elementRegistry) {
var callActivityGfx = elementRegistry.getGraphics('CallActivity'); var callActivityGfx = elementRegistry.getGraphics('CallActivity');
@ -191,7 +187,7 @@ describe('draw - bpmn renderer', function() {
// make sure the + marker is shown // make sure the + marker is shown
expect(domQuery('[data-marker=sub-process]', callActivityGfx)).to.exist; expect(domQuery('[data-marker=sub-process]', callActivityGfx)).to.exist;
done(); done(err);
})(); })();
}); });
@ -203,10 +199,6 @@ describe('draw - bpmn renderer', function() {
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
inject(function(elementRegistry) { inject(function(elementRegistry) {
var callActivityGfx = elementRegistry.getGraphics('AdHocSubProcess'); var callActivityGfx = elementRegistry.getGraphics('AdHocSubProcess');
@ -214,7 +206,7 @@ describe('draw - bpmn renderer', function() {
// make sure the + marker is shown // make sure the + marker is shown
expect(domQuery('[data-marker=adhoc]', callActivityGfx)).to.exist; expect(domQuery('[data-marker=adhoc]', callActivityGfx)).to.exist;
done(); done(err);
})(); })();
}); });
@ -226,10 +218,6 @@ describe('draw - bpmn renderer', function() {
var xml = require('../../fixtures/bpmn/simple.bpmn'); var xml = require('../../fixtures/bpmn/simple.bpmn');
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
inject(function(canvas) { inject(function(canvas) {
var svg = canvas._svg; var svg = canvas._svg;
var markers = svg.querySelectorAll('marker'); var markers = svg.querySelectorAll('marker');
@ -237,7 +225,7 @@ describe('draw - bpmn renderer', function() {
expect(markers[0].id).to.match(/^sequenceflow-end-white-black-[A-Za-z0-9]+$/); expect(markers[0].id).to.match(/^sequenceflow-end-white-black-[A-Za-z0-9]+$/);
})(); })();
done(); done(err);
}); });
}); });
@ -247,10 +235,6 @@ describe('draw - bpmn renderer', function() {
var xml = require('../../fixtures/bpmn/draw/colors.bpmn'); var xml = require('../../fixtures/bpmn/draw/colors.bpmn');
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
inject(function(canvas) { inject(function(canvas) {
var svg = canvas._svg; var svg = canvas._svg;
var markers = svg.querySelectorAll('marker'); var markers = svg.querySelectorAll('marker');
@ -260,7 +244,7 @@ describe('draw - bpmn renderer', function() {
expect(markers[4].id).to.match(/^messageflow-start-white-fuchsia-[A-Za-z0-9]{25}$/); expect(markers[4].id).to.match(/^messageflow-start-white-fuchsia-[A-Za-z0-9]{25}$/);
})(); })();
done(); done(err);
}); });
}); });

View File

@ -246,10 +246,6 @@ describe('label bounds', function() {
modeler.saveXML({ format: true }, function(err, result) { modeler.saveXML({ format: true }, function(err, result) {
if (err) {
return done(err);
}
// strip spaces and line breaks after '>' // strip spaces and line breaks after '>'
result = result.replace(/>\s+/g,'>'); result = result.replace(/>\s+/g,'>');
@ -262,7 +258,7 @@ describe('label bounds', function() {
expect(width).to.equal(shape.label.width); expect(width).to.equal(shape.label.width);
expect(height).to.equal(shape.label.height); expect(height).to.equal(shape.label.height);
done(); done(err);
}); });
}); });
}); });
@ -289,10 +285,6 @@ describe('label bounds', function() {
modeler.saveXML({ format: true }, function(err, result) { modeler.saveXML({ format: true }, function(err, result) {
if (err) {
return done(err);
}
// strip spaces and line breaks after '>' // strip spaces and line breaks after '>'
result = result.replace(/>\s+/g,'>'); result = result.replace(/>\s+/g,'>');
@ -305,7 +297,7 @@ describe('label bounds', function() {
expect(width).to.equal(shape.label.width); expect(width).to.equal(shape.label.width);
expect(height).to.equal(shape.label.height); expect(height).to.equal(shape.label.height);
done(); done(err);
}); });
}); });
}); });
@ -332,7 +324,7 @@ describe('label bounds', function() {
expect(result).to.equal(xml); expect(result).to.equal(xml);
done(); done(err);
}); });
}); });
}); });

View File

@ -7,7 +7,7 @@ var TestContainer = require('mocha-test-container-support');
var Diagram = require('diagram-js/lib/Diagram'), var Diagram = require('diagram-js/lib/Diagram'),
BpmnModdle = require('bpmn-moddle'), BpmnModdle = require('bpmn-moddle'),
Importer = require('../../../lib/import/Importer'), importBpmnDiagram = require('../../../lib/import/Importer').importBpmnDiagram,
Viewer = require('../../../lib/Viewer'); Viewer = require('../../../lib/Viewer');
var find = require('lodash/collection/find'); var find = require('lodash/collection/find');
@ -34,11 +34,7 @@ describe('import - Importer', function() {
var moddle = new BpmnModdle(); var moddle = new BpmnModdle();
moddle.fromXML(xml, function(err, definitions) { moddle.fromXML(xml, function(err, definitions) {
if (err) { importBpmnDiagram(diagram, definitions, done);
return done(err);
}
Importer.importBpmnDiagram(diagram, definitions, done);
}); });
} }

View File

@ -16,10 +16,6 @@ describe('import - associations', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// when // when
inject(function(elementRegistry) { inject(function(elementRegistry) {
@ -28,7 +24,7 @@ describe('import - associations', function() {
// then // then
expect(association).to.exist; expect(association).to.exist;
done(); done(err);
})(); })();
}); });
@ -42,10 +38,6 @@ describe('import - associations', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// when // when
inject(function(elementRegistry) { inject(function(elementRegistry) {
@ -90,17 +82,13 @@ describe('import - associations', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// then // then
expectRendered([ expectRendered([
'DataInputAssociation', 'DataInputAssociation',
'DataOutputAssociation' 'DataOutputAssociation'
]); ]);
done(); done(err);
}); });
}); });
@ -112,17 +100,13 @@ describe('import - associations', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// then // then
expectRendered([ expectRendered([
'DataInputAssociation', 'DataInputAssociation',
'DataOutputAssociation' 'DataOutputAssociation'
]); ]);
done(); done(err);
}); });
}); });
@ -134,18 +118,13 @@ describe('import - associations', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// then // then
expectRendered([ expectRendered([
'DataInputAssociation', 'DataInputAssociation',
'DataOutputAssociation' 'DataOutputAssociation'
]); ]);
done(); done(err);
}); });
}); });
@ -157,17 +136,13 @@ describe('import - associations', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// then // then
expectRendered([ expectRendered([
'DataInputAssociation', 'DataInputAssociation',
'DataOutputAssociation' 'DataOutputAssociation'
]); ]);
done(); done(err);
}); });
}); });

View File

@ -44,10 +44,6 @@ describe('import - labels', function() {
// given // given
bootstrapViewer(xml)(function(err) { bootstrapViewer(xml)(function(err) {
if (err) {
return done(err);
}
// when // when
inject(function(elementRegistry) { inject(function(elementRegistry) {
@ -68,7 +64,7 @@ describe('import - labels', function() {
expect(sequenceFlowLabel.width).to.be.above(64); expect(sequenceFlowLabel.width).to.be.above(64);
expect(sequenceFlowLabel.height).to.be.above(11); expect(sequenceFlowLabel.height).to.be.above(11);
done(); done(err);
})(); })();
}); });