diff --git a/gh-badge.js b/gh-badge.js index 92e8319f5cfa6..078ef01a81f3e 100755 --- a/gh-badge.js +++ b/gh-badge.js @@ -62,8 +62,13 @@ if (color[0] === ':') { badge(badgeData, function produceOutput(svg) { if (/png|jpg|gif/.test(format)) { - svg2img(svg, format, function (data) { - process.stdout.write(data); + svg2img(svg, format, function (err, data) { + if (err) { + console.error(err); + process.exit(1); + } else { + process.stdout.write(data); + } }); } else { console.log(svg); diff --git a/lib/svg-to-img.js b/lib/svg-to-img.js index 365a0fda53381..684e3ea515716 100644 --- a/lib/svg-to-img.js +++ b/lib/svg-to-img.js @@ -11,7 +11,7 @@ module.exports = function (svg, format, callback) { if (imgCache.has(cacheIndex)) { // We own a cache for this svg conversion. var result = imgCache.get(cacheIndex); - callback(result); + callback(null, result); return; } @@ -20,15 +20,13 @@ module.exports = function (svg, format, callback) { .density(90) .background(format === 'jpg' ? '#FFFFFF' : 'none') .flatten() - .stream(format, function (err, stdout, stderr) { - if (err) { console.error(err); return; } - var chunks = []; - stdout.on('data', function(chunk) { chunks.push(chunk); }); - stdout.on('finish', function() { - var result = Buffer.concat(chunks); - imgCache.set(cacheIndex, result); - callback(result); - }); + .toBuffer(format, function (err, data) { + if (err) { + callback(err); + } else { + imgCache.set(cacheIndex, data); + callback(null, data); + } }); }; diff --git a/public/500.html b/public/500.html new file mode 100644 index 0000000000000..25b4ff148601a --- /dev/null +++ b/public/500.html @@ -0,0 +1,6 @@ +A server error occurred + +

I have nothing to offer for this request

+

… but blood, toil, tears and sweat. +

+

And trying again later.

diff --git a/server.js b/server.js index 271f4ba016025..3f6eb95c15df0 100644 --- a/server.js +++ b/server.js @@ -6127,8 +6127,13 @@ function sendSVG(res, askres, end) { function sendOther(format, res, askres, end) { askres.setHeader('Content-Type', 'image/' + format); - svg2img(res, format, function (data) { - end(null, {template: streamFromString(data)}); + svg2img(res, format, function (err, data) { + if (err) { + console.error('svg2img error', err); + end(null, {template: '500.html'}); + } else { + end(null, {template: streamFromString(data)}); + } }); } diff --git a/test/svg-to-img.spec.js b/test/svg-to-img.spec.js index 2b19f92af4959..54e0443637475 100644 --- a/test/svg-to-img.spec.js +++ b/test/svg-to-img.spec.js @@ -12,7 +12,8 @@ describe('The rasterizer', function () { it('should produce PNG', function(done) { badge({ text: ['cactus', 'grown'], format: 'svg' }, svg => { - svg2img(svg, 'png', data => { + svg2img(svg, 'png', (err, data) => { + assert.equal(err, null); assert.ok(isPng(data)); done(); }); @@ -21,12 +22,14 @@ describe('The rasterizer', function () { it('should cache its results', function(done) { badge({ text: ['will-this', 'be-cached?'], format: 'svg' }, svg => { - svg2img(svg, 'png', data1 => { - assert.ok(isPng(data1)); + svg2img(svg, 'png', (err, data) => { + assert.equal(err, null); + assert.ok(isPng(data)); assert.equal(cacheGet.called, false); - svg2img(svg, 'png', data2 => { - assert.ok(isPng(data2)); + svg2img(svg, 'png', (err, data) => { + assert.equal(err, null); + assert.ok(isPng(data)); assert.ok(cacheGet.calledOnce); done();