Easy error subclassing and stack customization
Installations
npm install error-ex
Releases
Unable to fetch releases
Developer
qix-
Developer Guide
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
No
Node Version
9.6.1
NPM Version
5.6.0
Statistics
66 Stars
49 Commits
14 Forks
4 Watching
2 Branches
5 Contributors
Updated on 10 Nov 2024
Bundle Size
1.59 kB
Minified
763.00 B
Minified + Gzipped
Languages
CoffeeScript (67.7%)
JavaScript (32.3%)
Total Downloads
Cumulative downloads
Total Downloads
7,017,472,654
Last day
-4.9%
7,404,618
Compared to previous day
Last week
1.1%
39,581,068
Compared to previous week
Last month
11.8%
166,184,301
Compared to previous month
Last year
12.4%
1,761,126,731
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
node-error-ex
Easily subclass and customize new Error types
Examples
To include in your project:
1var errorEx = require('error-ex');
To create an error message type with a specific name (note, that ErrorFn.name
will not reflect this):
1var JSONError = errorEx('JSONError'); 2 3var err = new JSONError('error'); 4err.name; //-> JSONError 5throw err; //-> JSONError: error
To add a stack line:
1var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')}); 2 3var err = new JSONError('error') 4err.fileName = '/a/b/c/foo.json'; 5throw err; //-> (line 2)-> in /a/b/c/foo.json
To append to the error message:
1var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')}); 2 3var err = new JSONError('error'); 4err.fileName = '/a/b/c/foo.json'; 5throw err; //-> JSONError: error in /a/b/c/foo.json
API
errorEx([name], [properties])
Creates a new ErrorEx error type
name
: the name of the new type (appears in the error message upon throw; defaults toError.name
)properties
: if supplied, used as a key/value dictionary of properties to use when building up the stack message. Keys are property names that are looked up on the error message, and then passed to function values.line
: if specified and is a function, return value is added as a stack entry (error-ex will indent for you). Passed the property value given the key.stack
: if specified and is a function, passed the value of the property using the key, and the raw stack lines as a second argument. Takes no return value (but the stack can be modified directly).message
: if specified and is a function, return value is used as new.message
value upon get. Passed the property value of the property named by key, and the existing message is passed as the second argument as an array of lines (suitable for multi-line messages).
Returns a constructor (Function) that can be used just like the regular Error constructor.
1var errorEx = require('error-ex'); 2 3var BasicError = errorEx(); 4 5var NamedError = errorEx('NamedError'); 6 7// -- 8 9var AdvancedError = errorEx('AdvancedError', { 10 foo: { 11 line: function (value, stack) { 12 if (value) { 13 return 'bar ' + value; 14 } 15 return null; 16 } 17 } 18}) 19 20var err = new AdvancedError('hello, world'); 21err.foo = 'baz'; 22throw err; 23 24/* 25 AdvancedError: hello, world 26 bar baz 27 at tryReadme() (readme.js:20:1) 28*/
errorEx.line(str)
Creates a stack line using a delimiter
This is a helper function. It is to be used in lieu of writing a value object for
properties
values.
str
: The string to create- Use the delimiter
%s
to specify where in the string the value should go
- Use the delimiter
1var errorEx = require('error-ex'); 2 3var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')}); 4 5var err = new FileError('problem reading file'); 6err.fileName = '/a/b/c/d/foo.js'; 7throw err; 8 9/* 10 FileError: problem reading file 11 in /a/b/c/d/foo.js 12 at tryReadme() (readme.js:7:1) 13*/
errorEx.append(str)
Appends to the error.message
string
This is a helper function. It is to be used in lieu of writing a value object for
properties
values.
str
: The string to append- Use the delimiter
%s
to specify where in the string the value should go
- Use the delimiter
1var errorEx = require('error-ex'); 2 3var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')}); 4 5var err = new SyntaxError('improper indentation'); 6err.fileName = '/a/b/c/d/foo.js'; 7throw err; 8 9/* 10 SyntaxError: improper indentation in /a/b/c/d/foo.js 11 at tryReadme() (readme.js:7:1) 12*/
License
Licensed under the MIT License. You can find a copy of it in LICENSE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 4/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More