Repository: svg-sprite/svg-sprite (js)

Issue:
Remove `Error.errno`
And probably Error.name and just throw an `Error` with the proper message

Pull request description:
Replaced `Error.errno` with Error classes
Closes #645 

Patch:
diff --git a/lib/svg-sprite.js b/lib/svg-sprite.js
index 8d77508..a96ba61 100644
--- a/lib/svg-sprite.js
+++ b/lib/svg-sprite.js
@@ -25,6 +25,7 @@ const Queue = require('./svg-sprite/queue.js');
 const Layouter = require('./svg-sprite/layouter.js');
 const svgo = require('./svg-sprite/transform/svgo.js');
 const { isFunction, isObject, isPlainObject, zipObject } = require('./svg-sprite/utils/index.js');
+const ArgumentError = require('./svg-sprite/errors/argument-error.js');
 
 // TODO: after Node.js 12.x, `os.cpus` should never be undefined
 const CPU_COUNT = os.cpus() && os.cpus().length;
@@ -112,9 +113,7 @@ SVGSpriter.prototype.add = function(file, name, svg) {
 
         // In case of an error: Throw it!
         if (error) {
-            const e = new Error(error);
-            e.name = 'ArgumentError';
-            e.errno = 1_419_945_903;
+            const e = new ArgumentError(error);
             this.error(error, e);
             throw e;
         }
diff --git a/lib/svg-sprite/errors/argument-error.js b/lib/svg-sprite/errors/argument-error.js
new file mode 100644
index 0000000..e059363
--- /dev/null
+++ b/lib/svg-sprite/errors/argument-error.js
@@ -0,0 +1,3 @@
+module.exports = class ArgumentError extends Error {
+    name = 'ArgumentError';
+};
diff --git a/lib/svg-sprite/errors/dimensions-calculation-error.js b/lib/svg-sprite/errors/dimensions-calculation-error.js
new file mode 100644
index 0000000..3fcd1ea
--- /dev/null
+++ b/lib/svg-sprite/errors/dimensions-calculation-error.js
@@ -0,0 +1,3 @@
+module.exports = class DimensionsCalculationError extends Error {
+    name = 'DimensionsCalculationError';
+};
diff --git a/lib/svg-sprite/errors/not-permitted-error.js b/lib/svg-sprite/errors/not-permitted-error.js
new file mode 100644
index 0000000..5291532
--- /dev/null
+++ b/lib/svg-sprite/errors/not-permitted-error.js
@@ -0,0 +1,3 @@
+module.exports = class NotPermittedError extends Error {
+    name = 'NotPermittedError';
+};
diff --git a/lib/svg-sprite/errors/xml-fixing-error.js b/lib/svg-sprite/errors/xml-fixing-error.js
new file mode 100644
index 0000000..9785149
--- /dev/null
+++ b/lib/svg-sprite/errors/xml-fixing-error.js
@@ -0,0 +1,3 @@
+module.exports = class XmlFixingError extends Error {
+    name = 'XmlFixingError';
+};
diff --git a/lib/svg-sprite/shape.js b/lib/svg-sprite/shape.js
index bdb35bc..394dfbd 100644
--- a/lib/svg-sprite/shape.js
+++ b/lib/svg-sprite/shape.js
@@ -19,6 +19,8 @@ const { CssSelectorParser } = require('css-selector-parser');
 const async = require('async');
 const fixXMLString = require('./utils/fix-xml-string.js');
 const { isFunction, isString } = require('./utils/index.js');
+const ArgumentError = require('./errors/argument-error.js');
+const NotPermittedError = require('./errors/not-permitted-error.js');
 
 const DEFAULT_XML_DECLARATION = '<?xml version="1.0" encoding="utf-8"?>';
 
@@ -321,10 +323,7 @@ SVGShape.prototype._initSVG = function() {
 
     if (!svgStart) {
         const throwError = () => {
-            const e = new Error('Invalid SVG file');
-            e.name = 'ArgumentError';
-            e.errno = 1_429_395_394;
-            throw e;
+            throw new ArgumentError('Invalid SVG file');
         };
 
         try {
@@ -370,10 +369,7 @@ SVGShape.prototype._initSVG = function() {
     this.dom = new DOMParser({
         locator: {},
         errorHandler(level, message) {
-            const e = new Error(format('Invalid SVG file (%s)', message.split('\n').join(' ')));
-            e.name = 'ArgumentError';
-            e.errno = 1_429_394_706;
-            throw e;
+            throw new ArgumentError(format('Invalid SVG file (%s)', message.split('\n').join(' ')));
         }
     }).parseFromString(this.svg.current);
 
@@ -683,10 +679,7 @@ SVGShape.prototype.setNamespace = function(ns) {
     if (!this._namespaced && (namespaceIds || namespaceClassnames)) {
         // Ensure the shape has been complemented before
         if (!this.svg.ready) {
-            const error = new Error('Shape namespace cannot be set before complementing');
-            error.name = 'NotPermittedError';
-            error.errno = 1_419_162_245;
-            throw error;
+            throw new NotPermittedError('Shape namespace cannot be set before complementing');
         }
 
         const select = xpath.useNamespaces({ svg: this.DEFAULT_SVG_NAMESPACE, xlink: this.XLINK_NAMESPACE });
diff --git a/lib/svg-sprite/utils/calculate-svg-dimensions.js b/lib/svg-sprite/utils/calculate-svg-dimensions.js
index 36bdd50..42b72ff 100644
--- a/lib/svg-sprite/utils/calculate-svg-dimensions.js
+++ b/lib/svg-sprite/utils/calculate-svg-dimensions.js
@@ -1,6 +1,7 @@
 'use strict';
 
 const { Resvg } = require('@resvg/resvg-js');
+const DimensionsCalculationError = require('../errors/dimensions-calculation-error.js');
 
 /**
  * @typedef {object} Dimension
@@ -25,10 +26,7 @@ function calculateSvgDimensions(svg) {
 
         return { width, height };
     } catch (error) {
-        const e = new Error(error);
-        e.name = 'DimensionsCalculationError';
-        e.errno = 1_400_356_219;
-        throw e;
+        throw new DimensionsCalculationError(error);
     }
 }
 
diff --git a/lib/svg-sprite/utils/fix-xml-string.js b/lib/svg-sprite/utils/fix-xml-string.js
index 1fb6a45..5d255c4 100644
--- a/lib/svg-sprite/utils/fix-xml-string.js
+++ b/lib/svg-sprite/utils/fix-xml-string.js
@@ -1,6 +1,7 @@
 'use strict';
 
 const { DOMParser } = require('@xmldom/xmldom');
+const XmlFixingError = require('../errors/xml-fixing-error.js');
 
 /**
  * @param {string} svgString    svg string to fix
@@ -21,8 +22,5 @@ module.exports = svgString => {
         return fixedSVG;
     }
 
-    const error = new Error('Invalid XML string');
-    error.name = 'XMLFixingError';
-    error.errno = 1_400_141_400;
-    throw error;
+    throw new XmlFixingError('Invalid XML string');
 };


Test patch:
diff --git a/test/calculate-svg-dimensions.test.js b/test/calculate-svg-dimensions.test.js
index b37ac41..3fdfbdb 100644
--- a/test/calculate-svg-dimensions.test.js
+++ b/test/calculate-svg-dimensions.test.js
@@ -3,6 +3,7 @@
 const { readFile } = require('fs').promises;
 const path = require('path');
 const calculateSvgDimensions = require('../lib/svg-sprite/utils/calculate-svg-dimensions.js');
+const DimensionsCalculationError = require('../lib/svg-sprite/errors/dimensions-calculation-error.js');
 
 describe('calculateSvgDimensions', () => {
     it('should return the expected dimensions from 46x46 fixture', async() => {
@@ -87,6 +88,6 @@ describe('calculateSvgDimensions', () => {
 
         expect(() => {
             calculateSvgDimensions('');
-        }).toThrow(Error);
+        }).toThrow(DimensionsCalculationError);
     });
 });
diff --git a/test/fix-xml-string.test.js b/test/fix-xml-string.test.js
index b9a669f..6a63f97 100644
--- a/test/fix-xml-string.test.js
+++ b/test/fix-xml-string.test.js
@@ -3,6 +3,7 @@
 /* eslint-disable jest/prefer-expect-assertions */
 
 const fixXMLString = require('../lib/svg-sprite/utils/fix-xml-string.js');
+const XmlFixingError = require('../lib/svg-sprite/errors/xml-fixing-error.js');
 
 describe('testing fixing svg string', () => {
     it('should return valid svg file on svg with one multiline attribute values', () => {
@@ -37,6 +38,6 @@ describe('testing fixing svg string', () => {
     it('should throw an error on invalid file', () => {
         expect(() => {
             fixXMLString('<svg viewBox=></svg>');
-        }).toThrow(Error);
+        }).toThrow(XmlFixingError);
     });
 });
diff --git a/test/svg-shape.test.js b/test/svg-shape.test.js
index 540df42..a20e2b8 100644
--- a/test/svg-shape.test.js
+++ b/test/svg-shape.test.js
@@ -8,6 +8,7 @@ const glob = require('glob');
 const getShape = require('../lib/svg-sprite/shape.js');
 const SVGSpriter = require('../lib/svg-sprite.js');
 const fixXMLString = require('../lib/svg-sprite/utils/fix-xml-string.js');
+const ArgumentError = require('../lib/svg-sprite/errors/argument-error.js');
 
 jest.mock('../lib/svg-sprite/utils/fix-xml-string.js', () => jest.fn());
 
@@ -36,7 +37,7 @@ describe('testing SVGShape initialization', () => {
                 path: __dirname,
                 contents: Buffer.from(TEST_SVG)
             }), spriter);
-        }).not.toThrow(Error);
+        }).not.toThrow(ArgumentError);
         expect(fixXMLString).toHaveBeenCalledWith(TEST_SVG);
     });
 
@@ -52,7 +53,7 @@ describe('testing SVGShape initialization', () => {
                 path: __dirname,
                 contents: Buffer.from(TEST_SVG)
             }), spriter);
-        }).toThrow(new Error('Invalid SVG file'));
+        }).toThrow(new ArgumentError('Invalid SVG file'));
         expect(fixXMLString).toHaveBeenCalledWith(TEST_SVG);
     });
 
@@ -66,7 +67,7 @@ describe('testing SVGShape initialization', () => {
                 path: __dirname,
                 contents: Buffer.from(TEST_NON_SVG)
             }), spriter);
-        }).toThrow(Error);
+        }).toThrow(ArgumentError);
         expect(fixXMLString).toHaveBeenCalledWith(TEST_NON_SVG);
     });
 
@@ -86,7 +87,7 @@ describe('testing SVGShape initialization', () => {
                     path: __dirname,
                     contents: svgFileBuffer
                 }), spriter);
-            }).not.toThrow(Error);
+            }).not.toThrow(ArgumentError);
             expect(fixXMLString).not.toHaveBeenCalled();
         });
     });
diff --git a/test/svg-sprite/shape/shape.complement.test.js b/test/svg-sprite/shape/shape.complement.test.js
index 12b1d57..6a1424b 100644
--- a/test/svg-sprite/shape/shape.complement.test.js
+++ b/test/svg-sprite/shape/shape.complement.test.js
@@ -20,6 +20,7 @@ const TEST_FILE = {
 
 const SVGShape = require('../../../lib/svg-sprite/shape.js');
 const calculateSvgDimensions = require('../../../lib/svg-sprite/utils/calculate-svg-dimensions.js');
+const DimensionsCalculationError = require('../../../lib/svg-sprite/errors/dimensions-calculation-error.js');
 
 jest.mock('../../../lib/svg-sprite/utils/calculate-svg-dimensions');
 
@@ -115,11 +116,11 @@ describe('testing _determineDimensions()', () => {
         shape.height = 0;
 
         calculateSvgDimensions.mockImplementation(() => {
-            throw new Error(TEST_ERROR);
+            throw new DimensionsCalculationError(TEST_ERROR);
         });
         shape._determineDimensions(cb);
 
-        expect(cb).toHaveBeenCalledWith(new Error(TEST_ERROR));
+        expect(cb).toHaveBeenCalledWith(new DimensionsCalculationError(TEST_ERROR));
     });
 });
 
diff --git a/test/svg-sprite/shape/shape.svg.namespace.test.js b/test/svg-sprite/shape/shape.svg.namespace.test.js
index 061f3a7..dc1acc4 100644
--- a/test/svg-sprite/shape/shape.svg.namespace.test.js
+++ b/test/svg-sprite/shape/shape.svg.namespace.test.js
@@ -2,6 +2,7 @@
 
 const xpath = require('xpath');
 const SVGShape = require('../../../lib/svg-sprite/shape.js');
+const NotPermittedError = require('../../../lib/svg-sprite/errors/not-permitted-error.js');
 
 const TEST_SPRITER = {
     config: {
@@ -52,7 +53,7 @@ describe('testing setNamespace()', () => {
 
         expect(() => {
             shape.setNamespace({});
-        }).toThrow(new Error('Shape namespace cannot be set before complementing'));
+        }).toThrow(new NotPermittedError('Shape namespace cannot be set before complementing'));
     });
 
     describe('if namespaceIds', () => {