Socket
Socket
Sign inDemoInstall

err-code

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

err-code - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

.eslintrc.json

6

index.js
'use strict';
function createError(msg, code, props) {
var err = msg instanceof Error ? msg : new Error(msg);
var err = msg instanceof Error ? msg : new Error(msg);
var key;
if (code != null) {
if (typeof code === 'object') {
props = code;
} else if (code != null) {
err.code = code;

@@ -9,0 +11,0 @@ }

{
"name": "err-code",
"version": "1.0.0",
"version": "1.1.0",
"description": "Create an error with a code",
"main": "index.js",
"scripts": {
"test": "mocha -R spec"
"test": "mocha --bail",
"browserify": "browserify -s err-code index.js > index.umd.js"
},

@@ -26,5 +27,7 @@ "bugs": {

"devDependencies": {
"mocha": "^2.3.4",
"expect.js": "^0.3.1"
"@satazor/eslint-config": "^1.0.8",
"browserify": "^13.0.0",
"expect.js": "^0.3.1",
"mocha": "^2.3.4"
}
}

@@ -23,3 +23,5 @@ # err-code

The browser file is named index.umd.js which supports CommonJS, AMD and globals (errCode).
## Why

@@ -32,2 +34,3 @@

err.code = 'ESOMECODE';
err.detail = 'Additional information about the error';
throw err;

@@ -44,22 +47,22 @@ ```

throw errcode('My message', 'ESOMECODE');
```
// fill error with message + code
throw errcode(new Error('My message'), 'ESOMECODE');
// fill error with message + code + props
throw errcode(new Error('My message'), 'ESOMECODE', { detail: 'Additional information about the error' });
// fill error with message + props
throw errcode(new Error('My message'), { detail: 'Additional information about the error' });
Other custom properties
```js
var errcode = require('err-code');
// You may also pass a string in the first argument and an error will be automatically created
// for you, but stack trace will contain err-code so it's not advisable unless you don't really care.
throw errcode('My message', 'ESOMECODE', { some: 'property' });
// create error with message + code
throw errcode('My message', 'ESOMECODE');
// create error with message + code + props
throw errcode('My message', 'ESOMECODE', { detail: 'Additional information about the error' });
// create error with message + props
throw errcode('My message', { detail: 'Additional information about the error' });
```
Fill error object with a code and properties
```js
var errcode = require('err-code');
throw errcode(new Error('My message'), 'ESOMECODE', { some: 'property' });
```
## Tests

@@ -66,0 +69,0 @@

@@ -7,43 +7,87 @@ 'use strict';

describe('errcode', function () {
it('should create an error object without code', function () {
var err = errcode('my message');
describe('string as first argument', function () {
it('should create an error object without code', function () {
var err = errcode('my message');
expect(err).to.be.an(Error);
expect(err.hasOwnProperty(err.code)).to.be(false);
});
expect(err).to.be.an(Error);
expect(err.hasOwnProperty(err.code)).to.be(false);
});
it('should create an error object with code', function () {
var err = errcode('my message', 'ESOME');
it('should create an error object with code', function () {
var err = errcode('my message', 'ESOME');
expect(err).to.be.an(Error);
expect(err.code).to.be('ESOME');
});
expect(err).to.be.an(Error);
expect(err.code).to.be('ESOME');
});
it('should create an error object with code and properties', function () {
var err = errcode('my message', 'ESOME', { foo: 'bar', bar: 'foo' });
it('should create an error object with code and properties', function () {
var err = errcode('my message', 'ESOME', { foo: 'bar', bar: 'foo' });
expect(err).to.be.an(Error);
expect(err.code).to.be('ESOME');
expect(err.foo).to.be('bar');
expect(err.bar).to.be('foo');
expect(err).to.be.an(Error);
expect(err.code).to.be('ESOME');
expect(err.foo).to.be('bar');
expect(err.bar).to.be('foo');
});
it('should create an error object without code but with properties', function () {
var err = errcode('my message', { foo: 'bar', bar: 'foo' });
expect(err).to.be.an(Error);
expect(err.code).to.be(undefined);
expect(err.foo).to.be('bar');
expect(err.bar).to.be('foo');
});
});
it('should accept an error object and add code/properties', function () {
var myError = new Error('my message'),
err = errcode(myError, 'ESOME', { foo: 'bar', bar: 'foo' });
describe('error as first argument', function () {
it('should accept an error and do nothing', function () {
var myErr = new Error('my message');
var err = errcode(myErr);
expect(err).to.be.an(Error);
expect(myError).to.be(err);
expect(err.code).to.be('ESOME');
expect(err.foo).to.be('bar');
expect(err.bar).to.be('foo');
expect(err).to.be(myErr);
expect(err.hasOwnProperty(err.code)).to.be(false);
});
it('should accept an error and add a code', function () {
var myErr = new Error('my message');
var err = errcode(myErr, 'ESOME');
expect(err).to.be(myErr);
expect(err.code).to.be('ESOME');
});
it('should accept an error object and add code & properties', function () {
var myErr = new Error('my message');
var err = errcode(myErr, 'ESOME', { foo: 'bar', bar: 'foo' });
expect(err).to.be.an(Error);
expect(err.code).to.be('ESOME');
expect(err.foo).to.be('bar');
expect(err.bar).to.be('foo');
});
it('should create an error object without code but with properties', function () {
var myErr = new Error('my message');
var err = errcode(myErr, { foo: 'bar', bar: 'foo' });
expect(err).to.be.an(Error);
expect(err.code).to.be(undefined);
expect(err.foo).to.be('bar');
expect(err.bar).to.be('foo');
});
});
it('should create an error object with code and without message', function () {
var err = errcode(null, 'ESOME');
it('should allow passing null & undefined in the first argument', function () {
var err;
err = errcode(null, 'ESOME');
expect(err).to.be.an(Error);
expect(err.message).to.be('null');
expect(err.code).to.be('ESOME');
err = errcode(undefined, 'ESOME');
expect(err).to.be.an(Error);
expect(err.message).to.be('');
expect(err.code).to.be('ESOME');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc