Skip to content

Commit

Permalink
Fix error handling now that canvas supports svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Feb 19, 2019
1 parent 4f447ad commit f5e8a95
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions lib/spriteBackgroundImages.js
Expand Up @@ -11,14 +11,7 @@ function getProperties(container, propertyName) {
async function getCanvasImageFromImageAsset(imageAsset) {
const canvasImage = new Image();
await new Promise((resolve, reject) => {
canvasImage.onerror = err => {
if (err.message.includes('node-canvas was built without SVG support')) {
err.message = 'Adding SVG images to a sprite is not possible';
}

err.message += ` (${imageAsset.urlOrDescription})`;
reject(err);
};
canvasImage.onerror = err => reject(err);
canvasImage.onload = resolve;
canvasImage.src = imageAsset.rawSrc;
});
Expand All @@ -27,7 +20,19 @@ async function getCanvasImageFromImageAsset(imageAsset) {

async function getImageAssetFromCanvas(canvas, assetType, assetGraph) {
if (assetType === 'Png') {
const rawSrc = await promisify(cb => canvas.toBuffer(cb))();
let rawSrc;
try {
rawSrc = await promisify(cb => canvas.toBuffer(cb))();
} catch (err) {
if (
err.message.includes(
'the surface type is not appropriate for the operation'
)
) {
err.message += ' (are you trying to add an SVG to a sprite?)';
}
throw err;
}
return {
type: 'Png',
rawSrc
Expand Down
2 changes: 1 addition & 1 deletion test/spriteBackgroundImages.js
Expand Up @@ -530,7 +530,7 @@ describe('spriteBackgroundImages', () => {
await expect(
assetGraph.queue(spriteBackgroundImages()),
'to be rejected with',
/Adding SVG images to a sprite is not possible/
/are you trying to add an SVG to a sprite/
);
});
});

0 comments on commit f5e8a95

Please sign in to comment.