Always call the callback + reliable erroring

Fixes #914
This commit is contained in:
Paul Melnikow 2017-04-06 10:17:07 -04:00 committed by Thaddee Tyl
parent 0760d17d82
commit 8b77d16a72
5 changed files with 36 additions and 19 deletions

View File

@ -62,8 +62,13 @@ if (color[0] === ':') {
badge(badgeData, function produceOutput(svg) { badge(badgeData, function produceOutput(svg) {
if (/png|jpg|gif/.test(format)) { if (/png|jpg|gif/.test(format)) {
svg2img(svg, format, function (data) { svg2img(svg, format, function (err, data) {
process.stdout.write(data); if (err) {
console.error(err);
process.exit(1);
} else {
process.stdout.write(data);
}
}); });
} else { } else {
console.log(svg); console.log(svg);

View File

@ -11,7 +11,7 @@ module.exports = function (svg, format, callback) {
if (imgCache.has(cacheIndex)) { if (imgCache.has(cacheIndex)) {
// We own a cache for this svg conversion. // We own a cache for this svg conversion.
var result = imgCache.get(cacheIndex); var result = imgCache.get(cacheIndex);
callback(result); callback(null, result);
return; return;
} }
@ -20,15 +20,13 @@ module.exports = function (svg, format, callback) {
.density(90) .density(90)
.background(format === 'jpg' ? '#FFFFFF' : 'none') .background(format === 'jpg' ? '#FFFFFF' : 'none')
.flatten() .flatten()
.stream(format, function (err, stdout, stderr) { .toBuffer(format, function (err, data) {
if (err) { console.error(err); return; } if (err) {
var chunks = []; callback(err);
stdout.on('data', function(chunk) { chunks.push(chunk); }); } else {
stdout.on('finish', function() { imgCache.set(cacheIndex, data);
var result = Buffer.concat(chunks); callback(null, data);
imgCache.set(cacheIndex, result); }
callback(result);
});
}); });
}; };

6
public/500.html Normal file
View File

@ -0,0 +1,6 @@
<!doctype html><meta charset=utf-8><title>A server error occurred</title>
<h1> I have nothing to offer for this request </h1>
<p> … but blood, toil, tears and sweat.
</p>
<p>And trying again later.</p>

View File

@ -6127,8 +6127,13 @@ function sendSVG(res, askres, end) {
function sendOther(format, res, askres, end) { function sendOther(format, res, askres, end) {
askres.setHeader('Content-Type', 'image/' + format); askres.setHeader('Content-Type', 'image/' + format);
svg2img(res, format, function (data) { svg2img(res, format, function (err, data) {
end(null, {template: streamFromString(data)}); if (err) {
console.error('svg2img error', err);
end(null, {template: '500.html'});
} else {
end(null, {template: streamFromString(data)});
}
}); });
} }

View File

@ -12,7 +12,8 @@ describe('The rasterizer', function () {
it('should produce PNG', function(done) { it('should produce PNG', function(done) {
badge({ text: ['cactus', 'grown'], format: 'svg' }, svg => { badge({ text: ['cactus', 'grown'], format: 'svg' }, svg => {
svg2img(svg, 'png', data => { svg2img(svg, 'png', (err, data) => {
assert.equal(err, null);
assert.ok(isPng(data)); assert.ok(isPng(data));
done(); done();
}); });
@ -21,12 +22,14 @@ describe('The rasterizer', function () {
it('should cache its results', function(done) { it('should cache its results', function(done) {
badge({ text: ['will-this', 'be-cached?'], format: 'svg' }, svg => { badge({ text: ['will-this', 'be-cached?'], format: 'svg' }, svg => {
svg2img(svg, 'png', data1 => { svg2img(svg, 'png', (err, data) => {
assert.ok(isPng(data1)); assert.equal(err, null);
assert.ok(isPng(data));
assert.equal(cacheGet.called, false); assert.equal(cacheGet.called, false);
svg2img(svg, 'png', data2 => { svg2img(svg, 'png', (err, data) => {
assert.ok(isPng(data2)); assert.equal(err, null);
assert.ok(isPng(data));
assert.ok(cacheGet.calledOnce); assert.ok(cacheGet.calledOnce);
done(); done();