Skip to content

Commit 420ae9d

Browse files
committed
Throw SvgoParserError
1 parent dd9cad1 commit 420ae9d

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

lib/svgo.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ const optimize = (input, config) => {
6363
}
6464
for (let i = 0; i < maxPassCount; i += 1) {
6565
info.multipassCount = i;
66-
// TODO throw this error in v3
67-
try {
68-
svgjs = parseSvg(input, config.path);
69-
} catch (error) {
70-
return { error: error.toString(), modernError: error };
71-
}
66+
svgjs = parseSvg(input, config.path);
7267
if (svgjs.error != null) {
7368
if (config.path != null) {
7469
svgjs.path = config.path;

lib/svgo.test.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -274,30 +274,38 @@ test('provides informative error in result', () => {
274274
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
275275
</svg>
276276
`;
277-
const { modernError: error } = optimize(svg, { path: 'test.svg' });
278-
expect(error.name).toEqual('SvgoParserError');
279-
expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value');
280-
expect(error.reason).toEqual('Unquoted attribute value');
281-
expect(error.line).toEqual(2);
282-
expect(error.column).toEqual(33);
283-
expect(error.source).toEqual(svg);
277+
try {
278+
optimize(svg, { path: 'test.svg' });
279+
expect(true).toEqual(false);
280+
} catch (error) {
281+
expect(error.name).toEqual('SvgoParserError');
282+
expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value');
283+
expect(error.reason).toEqual('Unquoted attribute value');
284+
expect(error.line).toEqual(2);
285+
expect(error.column).toEqual(33);
286+
expect(error.source).toEqual(svg);
287+
}
284288
});
285289

286290
test('provides code snippet in rendered error', () => {
287291
const svg = `<svg viewBox="0 0 120 120">
288292
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
289293
</svg>
290294
`;
291-
const { modernError: error } = optimize(svg, { path: 'test.svg' });
292-
expect(error.toString())
293-
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
295+
try {
296+
optimize(svg, { path: 'test.svg' });
297+
expect(true).toEqual(false);
298+
} catch (error) {
299+
expect(error.toString())
300+
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
294301
295302
1 | <svg viewBox="0 0 120 120">
296303
> 2 | <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
297304
| ^
298305
3 | </svg>
299306
4 |
300307
`);
308+
}
301309
});
302310

303311
test('supports errors without path', () => {
@@ -314,9 +322,12 @@ test('supports errors without path', () => {
314322
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
315323
</svg>
316324
`;
317-
const { modernError: error } = optimize(svg);
318-
expect(error.toString())
319-
.toEqual(`SvgoParserError: <input>:11:29: Unquoted attribute value
325+
try {
326+
optimize(svg);
327+
expect(true).toEqual(false);
328+
} catch (error) {
329+
expect(error.toString())
330+
.toEqual(`SvgoParserError: <input>:11:29: Unquoted attribute value
320331
321332
9 | <circle/>
322333
10 | <circle/>
@@ -325,40 +336,28 @@ test('supports errors without path', () => {
325336
12 | </svg>
326337
13 |
327338
`);
339+
}
328340
});
329341

330342
test('slices long line in error code snippet', () => {
331343
const svg = `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" viewBox="0 0 230 120">
332344
<path d="M318.198 551.135 530.33 918.56l-289.778-77.646 38.823-144.889c77.646-289.778 294.98-231.543 256.156-86.655s178.51 203.124 217.334 58.235q58.234-217.334 250.955 222.534t579.555 155.292z stroke-width="1.5" fill="red" stroke="red" />
333345
</svg>
334346
`;
335-
const { modernError: error } = optimize(svg);
336-
expect(error.toString())
337-
.toEqual(`SvgoParserError: <input>:2:211: Invalid attribute name
347+
try {
348+
optimize(svg);
349+
expect(true).toEqual(false);
350+
} catch (error) {
351+
expect(error.toString())
352+
.toEqual(`SvgoParserError: <input>:2:211: Invalid attribute name
338353
339354
1 | …-0.dtd" viewBox="0 0 230 120">
340355
> 2 | …7.334 250.955 222.534t579.555 155.292z stroke-width="1.5" fill="red" strok…
341356
| ^
342357
3 |
343358
4 |
344359
`);
345-
});
346-
347-
test('provides legacy error message', () => {
348-
const svg = `<svg viewBox="0 0 120 120">
349-
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
350-
</svg>
351-
`;
352-
const { error } = optimize(svg, { path: 'test.svg' });
353-
expect(error)
354-
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
355-
356-
1 | <svg viewBox="0 0 120 120">
357-
> 2 | <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
358-
| ^
359-
3 | </svg>
360-
4 |
361-
`);
360+
}
362361
});
363362

364363
test('multipass option should trigger plugins multiple times', () => {

lib/svgo/coa.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,16 @@ function processSVGData(config, info, data, output, input) {
385385
var startTime = Date.now(),
386386
prevFileSize = Buffer.byteLength(data, 'utf8');
387387

388-
const result = optimize(data, { ...config, ...info });
389-
if (result.modernError) {
390-
console.error(colors.red(result.modernError.toString()));
391-
process.exit(1);
388+
let result;
389+
try {
390+
result = optimize(data, { ...config, ...info });
391+
} catch (error) {
392+
if (error.name === 'SvgoParserError') {
393+
console.error(colors.red(error.toString()));
394+
process.exit(1);
395+
} else {
396+
throw error;
397+
}
392398
}
393399
if (config.datauri) {
394400
result.data = encodeSVGDatauri(result.data, config.datauri);

0 commit comments

Comments
 (0)