Skip to content

Commit

Permalink
Merge pull request #26 from krose422/fix-25
Browse files Browse the repository at this point in the history
fix: wrap canvg method in try / catch and pass error in response
  • Loading branch information
fuzhenn authored May 21, 2020
2 parents dbfaa7d + 944e403 commit d222242
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function svg2img(svg, options, callback) {
if (!format) {
format = 'png';
}
var canvas = convert(content, options);
var canvas = convert(content, options, callback);
var stream;
if (format === 'jpg' || format === 'jpeg') {
stream = canvas.jpegStream({
Expand All @@ -55,9 +55,13 @@ function svg2img(svg, options, callback) {
});
}

function convert(svgContent,options) {
function convert(svgContent, options, callback) {
var canvas = Canvas.createCanvas(options.width||100, options.height||100);
canvg(canvas, svgContent, { ignoreMouse: true, ignoreAnimation: true, ImageClass: Canvas.Image });
try {
canvg(canvas, svgContent, { ignoreMouse: true, ignoreAnimation: true, ImageClass: Canvas.Image });
} catch (error) {
callback(error);
}
return canvas;
}

Expand Down

0 comments on commit d222242

Please sign in to comment.