diff --git a/lib/svgo.js b/lib/svgo.js index 9064712b9..144417fa5 100644 --- a/lib/svgo.js +++ b/lib/svgo.js @@ -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; diff --git a/lib/svgo.test.js b/lib/svgo.test.js index 964250f05..ecf303608 100644 --- a/lib/svgo.test.js +++ b/lib/svgo.test.js @@ -274,13 +274,17 @@ test('provides informative error in result', () => { `; - 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', () => { @@ -288,9 +292,12 @@ test('provides code snippet in rendered error', () => { `; - 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 | > 2 | @@ -298,6 +305,7 @@ test('provides code snippet in rendered error', () => { 3 | 4 | `); + } }); test('supports errors without path', () => { @@ -314,9 +322,12 @@ test('supports errors without path', () => { `; - const { modernError: error } = optimize(svg); - expect(error.toString()) - .toEqual(`SvgoParserError: :11:29: Unquoted attribute value + try { + optimize(svg); + expect(true).toEqual(false); + } catch (error) { + expect(error.toString()) + .toEqual(`SvgoParserError: :11:29: Unquoted attribute value 9 | 10 | @@ -325,6 +336,7 @@ test('supports errors without path', () => { 12 | 13 | `); + } }); test('slices long line in error code snippet', () => { @@ -332,9 +344,12 @@ test('slices long line in error code snippet', () => { `; - const { modernError: error } = optimize(svg); - expect(error.toString()) - .toEqual(`SvgoParserError: :2:211: Invalid attribute name + try { + optimize(svg); + expect(true).toEqual(false); + } catch (error) { + expect(error.toString()) + .toEqual(`SvgoParserError: :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… @@ -342,23 +357,7 @@ test('slices long line in error code snippet', () => { 3 | 4 | `); -}); - -test('provides legacy error message', () => { - const svg = ` - - -`; - const { error } = optimize(svg, { path: 'test.svg' }); - expect(error) - .toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value - - 1 | -> 2 | - | ^ - 3 | - 4 | -`); + } }); test('multipass option should trigger plugins multiple times', () => { diff --git a/lib/svgo/coa.js b/lib/svgo/coa.js index ba82f2504..52d640c4a 100644 --- a/lib/svgo/coa.js +++ b/lib/svgo/coa.js @@ -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); diff --git a/test/coa/testSvg/test.1.svg b/test/coa/testSvg/test.1.svg index 43e40047b..1dcbceb77 100644 --- a/test/coa/testSvg/test.1.svg +++ b/test/coa/testSvg/test.1.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/test/coa/testSvg/test.svg b/test/coa/testSvg/test.svg index 43e40047b..1dcbceb77 100644 --- a/test/coa/testSvg/test.svg +++ b/test/coa/testSvg/test.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file