Skip to content

Commit

Permalink
Throw SvgoParserError
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Oct 9, 2022
1 parent dd9cad1 commit 420ae9d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
7 changes: 1 addition & 6 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ const optimize = (input, config) => {
}
for (let i = 0; i < maxPassCount; i += 1) {
info.multipassCount = i;
// TODO throw this error in v3
try {
svgjs = parseSvg(input, config.path);
} catch (error) {
return { error: error.toString(), modernError: error };
}
svgjs = parseSvg(input, config.path);
if (svgjs.error != null) {
if (config.path != null) {
svgjs.path = config.path;
Expand Down
65 changes: 32 additions & 33 deletions lib/svgo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,30 +274,38 @@ test('provides informative error in result', () => {
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
</svg>
`;
const { modernError: error } = optimize(svg, { path: 'test.svg' });
expect(error.name).toEqual('SvgoParserError');
expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value');
expect(error.reason).toEqual('Unquoted attribute value');
expect(error.line).toEqual(2);
expect(error.column).toEqual(33);
expect(error.source).toEqual(svg);
try {
optimize(svg, { path: 'test.svg' });
expect(true).toEqual(false);
} catch (error) {
expect(error.name).toEqual('SvgoParserError');
expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value');
expect(error.reason).toEqual('Unquoted attribute value');
expect(error.line).toEqual(2);
expect(error.column).toEqual(33);
expect(error.source).toEqual(svg);
}
});

test('provides code snippet in rendered error', () => {
const svg = `<svg viewBox="0 0 120 120">
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
</svg>
`;
const { modernError: error } = optimize(svg, { path: 'test.svg' });
expect(error.toString())
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
try {
optimize(svg, { path: 'test.svg' });
expect(true).toEqual(false);
} catch (error) {
expect(error.toString())
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
1 | <svg viewBox="0 0 120 120">
> 2 | <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
| ^
3 | </svg>
4 |
`);
}
});

test('supports errors without path', () => {
Expand All @@ -314,9 +322,12 @@ test('supports errors without path', () => {
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
</svg>
`;
const { modernError: error } = optimize(svg);
expect(error.toString())
.toEqual(`SvgoParserError: <input>:11:29: Unquoted attribute value
try {
optimize(svg);
expect(true).toEqual(false);
} catch (error) {
expect(error.toString())
.toEqual(`SvgoParserError: <input>:11:29: Unquoted attribute value
9 | <circle/>
10 | <circle/>
Expand All @@ -325,40 +336,28 @@ test('supports errors without path', () => {
12 | </svg>
13 |
`);
}
});

test('slices long line in error code snippet', () => {
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">
<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" />
</svg>
`;
const { modernError: error } = optimize(svg);
expect(error.toString())
.toEqual(`SvgoParserError: <input>:2:211: Invalid attribute name
try {
optimize(svg);
expect(true).toEqual(false);
} catch (error) {
expect(error.toString())
.toEqual(`SvgoParserError: <input>:2:211: Invalid attribute name
1 | …-0.dtd" viewBox="0 0 230 120">
> 2 | …7.334 250.955 222.534t579.555 155.292z stroke-width="1.5" fill="red" strok…
| ^
3 |
4 |
`);
});

test('provides legacy error message', () => {
const svg = `<svg viewBox="0 0 120 120">
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
</svg>
`;
const { error } = optimize(svg, { path: 'test.svg' });
expect(error)
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
1 | <svg viewBox="0 0 120 120">
> 2 | <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
| ^
3 | </svg>
4 |
`);
}
});

test('multipass option should trigger plugins multiple times', () => {
Expand Down
14 changes: 10 additions & 4 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,16 @@ function processSVGData(config, info, data, output, input) {
var startTime = Date.now(),
prevFileSize = Buffer.byteLength(data, 'utf8');

const result = optimize(data, { ...config, ...info });
if (result.modernError) {
console.error(colors.red(result.modernError.toString()));
process.exit(1);
let result;
try {
result = optimize(data, { ...config, ...info });
} catch (error) {
if (error.name === 'SvgoParserError') {
console.error(colors.red(error.toString()));
process.exit(1);
} else {
throw error;
}
}
if (config.datauri) {
result.data = encodeSVGDatauri(result.data, config.datauri);
Expand Down
Loading

0 comments on commit 420ae9d

Please sign in to comment.