Skip to content

Commit ea82cc2

Browse files
committed
Report parsing errors with filename
1 parent b988049 commit ea82cc2

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

lib/svgo.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ const optimize = (input, config) => {
4040
for (let i = 0; i < maxPassCount; i += 1) {
4141
info.multipassCount = i;
4242
svgjs = svg2js(input);
43-
if (svgjs.error == null) {
44-
const plugins = config.plugins || defaultPlugins;
45-
if (Array.isArray(plugins) === false) {
46-
throw Error('Invalid plugins list. Provided \'plugins\' in config should be an array.');
43+
if (svgjs.error != null) {
44+
if (config.path != null) {
45+
svgjs.path = config.path;
4746
}
48-
const resolvedPlugins = plugins.map(plugin => resolvePluginConfig(plugin, config))
49-
svgjs = invokePlugins(svgjs, info, resolvedPlugins);
47+
return svgjs;
48+
}
49+
const plugins = config.plugins || defaultPlugins;
50+
if (Array.isArray(plugins) === false) {
51+
throw Error('Invalid plugins list. Provided \'plugins\' in config should be an array.');
5052
}
53+
const resolvedPlugins = plugins.map(plugin => resolvePluginConfig(plugin, config))
54+
svgjs = invokePlugins(svgjs, info, resolvedPlugins);
5155
svgjs = js2svg(svgjs, config.js2svg);
5256
if (svgjs.error) {
5357
throw Error(svgjs.error);

lib/svgo/coa.js

+7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ function processSVGData(config, info, data, output, input) {
282282
prevFileSize = Buffer.byteLength(data, 'utf8');
283283

284284
const result = optimize(data, { ...config, ...info });
285+
if (result.error) {
286+
let message = result.error;
287+
if (result.path != null) {
288+
message += `\nFile: ${result.path}`
289+
}
290+
throw Error(message)
291+
}
285292
if (config.datauri) {
286293
result.data = encodeSVGDatauri(result.data, config.datauri);
287294
}

test/svgo/_index.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,40 @@ const parseFixture = async file => {
2121
describe('svgo', () => {
2222
it('should create indent with 2 spaces', async () => {
2323
const [original, expected] = await parseFixture('test.svg');
24-
const result = optimize(original, {
25-
plugins: [],
26-
js2svg: { pretty: true, indent: 2 },
27-
});
28-
expect(normalize(result.data)).to.equal(expected);
24+
const result = optimize(original, {
25+
plugins: [],
26+
js2svg: { pretty: true, indent: 2 },
27+
});
28+
expect(normalize(result.data)).to.equal(expected);
2929
});
3030
it('should run multiple times', async () => {
3131
const [original, expected] = await parseFixture('multipass.svg');
32-
const result = optimize(original, {
33-
multipass: true,
34-
});
35-
expect(normalize(result.data)).to.equal(expected);
32+
const result = optimize(original, {
33+
multipass: true,
34+
});
35+
expect(normalize(result.data)).to.equal(expected);
3636
});
3737
it('should pass multipass count to plugins', async () => {
3838
const [original, expected] = await parseFixture('multipass-prefix-ids.svg');
39-
const result = optimize(original, {
40-
multipass: true,
41-
plugins: extendDefaultPlugins([
42-
{
43-
name: 'prefixIds',
44-
},
45-
]),
46-
});
47-
expect(normalize(result.data)).to.equal(expected);
39+
const result = optimize(original, {
40+
multipass: true,
41+
plugins: extendDefaultPlugins([
42+
{
43+
name: 'prefixIds',
44+
},
45+
]),
46+
});
47+
expect(normalize(result.data)).to.equal(expected);
4848
});
4949
it('should handle plugins order properly', async () => {
5050
const [original, expected] = await parseFixture('plugins-order.svg');
51-
const result = optimize(original, { input: 'file', path: 'input.svg' });
52-
expect(normalize(result.data)).to.equal(expected);
51+
const result = optimize(original, { input: 'file', path: 'input.svg' });
52+
expect(normalize(result.data)).to.equal(expected);
53+
});
54+
it('should handle parse error', async () => {
55+
const fixture = await fs.promises.readFile(path.resolve(__dirname, 'invalid.svg'));
56+
const result = optimize(fixture, { input: 'file', path: 'input.svg' });
57+
expect(result.error).to.match(/Error in parsing SVG/);
58+
expect(result.path).to.equal('input.svg');
5359
});
5460
});

test/svgo/invalid.svg

+1
Loading

0 commit comments

Comments
 (0)