Skip to content

Commit

Permalink
Report parsing errors with filename
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Feb 19, 2021
1 parent b988049 commit ea82cc2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
16 changes: 10 additions & 6 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ const optimize = (input, config) => {
for (let i = 0; i < maxPassCount; i += 1) {
info.multipassCount = i;
svgjs = svg2js(input);
if (svgjs.error == null) {
const plugins = config.plugins || defaultPlugins;
if (Array.isArray(plugins) === false) {
throw Error('Invalid plugins list. Provided \'plugins\' in config should be an array.');
if (svgjs.error != null) {
if (config.path != null) {
svgjs.path = config.path;
}
const resolvedPlugins = plugins.map(plugin => resolvePluginConfig(plugin, config))
svgjs = invokePlugins(svgjs, info, resolvedPlugins);
return svgjs;
}
const plugins = config.plugins || defaultPlugins;
if (Array.isArray(plugins) === false) {
throw Error('Invalid plugins list. Provided \'plugins\' in config should be an array.');
}
const resolvedPlugins = plugins.map(plugin => resolvePluginConfig(plugin, config))
svgjs = invokePlugins(svgjs, info, resolvedPlugins);
svgjs = js2svg(svgjs, config.js2svg);
if (svgjs.error) {
throw Error(svgjs.error);
Expand Down
7 changes: 7 additions & 0 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ function processSVGData(config, info, data, output, input) {
prevFileSize = Buffer.byteLength(data, 'utf8');

const result = optimize(data, { ...config, ...info });
if (result.error) {
let message = result.error;
if (result.path != null) {
message += `\nFile: ${result.path}`
}
throw Error(message)
}
if (config.datauri) {
result.data = encodeSVGDatauri(result.data, config.datauri);
}
Expand Down
46 changes: 26 additions & 20 deletions test/svgo/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,40 @@ const parseFixture = async file => {
describe('svgo', () => {
it('should create indent with 2 spaces', async () => {
const [original, expected] = await parseFixture('test.svg');
const result = optimize(original, {
plugins: [],
js2svg: { pretty: true, indent: 2 },
});
expect(normalize(result.data)).to.equal(expected);
const result = optimize(original, {
plugins: [],
js2svg: { pretty: true, indent: 2 },
});
expect(normalize(result.data)).to.equal(expected);
});
it('should run multiple times', async () => {
const [original, expected] = await parseFixture('multipass.svg');
const result = optimize(original, {
multipass: true,
});
expect(normalize(result.data)).to.equal(expected);
const result = optimize(original, {
multipass: true,
});
expect(normalize(result.data)).to.equal(expected);
});
it('should pass multipass count to plugins', async () => {
const [original, expected] = await parseFixture('multipass-prefix-ids.svg');
const result = optimize(original, {
multipass: true,
plugins: extendDefaultPlugins([
{
name: 'prefixIds',
},
]),
});
expect(normalize(result.data)).to.equal(expected);
const result = optimize(original, {
multipass: true,
plugins: extendDefaultPlugins([
{
name: 'prefixIds',
},
]),
});
expect(normalize(result.data)).to.equal(expected);
});
it('should handle plugins order properly', async () => {
const [original, expected] = await parseFixture('plugins-order.svg');
const result = optimize(original, { input: 'file', path: 'input.svg' });
expect(normalize(result.data)).to.equal(expected);
const result = optimize(original, { input: 'file', path: 'input.svg' });
expect(normalize(result.data)).to.equal(expected);
});
it('should handle parse error', async () => {
const fixture = await fs.promises.readFile(path.resolve(__dirname, 'invalid.svg'));
const result = optimize(fixture, { input: 'file', path: 'input.svg' });
expect(result.error).to.match(/Error in parsing SVG/);
expect(result.path).to.equal('input.svg');
});
});
1 change: 1 addition & 0 deletions test/svgo/invalid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ea82cc2

Please sign in to comment.