Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

defekt

Package Overview
Dependencies
Maintainers
4
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

defekt - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

src/defekt.js

35

dist/defekt.js
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var humanizeString = require('humanize-string');
var defekt = function defekt(errorDefinitions) {

@@ -22,3 +38,3 @@ if (!errorDefinitions) {

switch (typeof errorDefinition === 'undefined' ? 'undefined' : _typeof(errorDefinition)) {
switch (typeof errorDefinition === 'undefined' ? 'undefined' : (0, _typeof3.default)(errorDefinition)) {
case 'string':

@@ -37,12 +53,11 @@ errorName = errorDefinition;

var CustomError = function (_Error) {
_inherits(CustomError, _Error);
(0, _inherits3.default)(CustomError, _Error);
function CustomError() {
var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : humanizeString(errorName) + '.';
var cause = arguments[1];
(0, _classCallCheck3.default)(this, CustomError);
_classCallCheck(this, CustomError);
var _this = (0, _possibleConstructorReturn3.default)(this, (CustomError.__proto__ || (0, _getPrototypeOf2.default)(CustomError)).call(this));
var _this = _possibleConstructorReturn(this, (CustomError.__proto__ || Object.getPrototypeOf(CustomError)).call(this));
_this.name = errorName;

@@ -49,0 +64,0 @@ _this.code = errorCode;

{
"name": "defekt",
"version": "1.0.0",
"version": "2.0.0",
"description": "defekt is custom errors made simple.",

@@ -20,7 +20,8 @@ "contributors": [

"main": "dist/defekt.js",
"dependencies": {},
"dependencies": {
"humanize-string": "1.0.2"
},
"devDependencies": {
"assertthat": "1.0.0",
"roboter": "0.15.6",
"roboter-server": "0.15.6"
"roboter": "1.1.0"
},

@@ -27,0 +28,0 @@ "repository": {

@@ -15,3 +15,3 @@ # defekt

First you need to add a reference to defekt in your application.
First you need to add a reference to defekt in your application:

@@ -22,3 +22,3 @@ ```javascript

Then call the `defekt` function and hand over an array of custom error names that you would like to have created.
Then call the `defekt` function and hand over an array of custom error names that you would like to have created:

@@ -33,3 +33,3 @@ ```javascript

The result is an object containing all the errors you specified. To use one of those errors, simply call the appropriate function with `new` and throw it.
The result is an object containing all the errors you specified. To use one of those errors, simply call the appropriate function with `new` and throw it:

@@ -40,9 +40,20 @@ ```javascript

If you want to specify a message, feel free to do so.
By default, the error name is also used as the error message, but converted to a human readable form. E.g., the error name `InvalidOperation` becomes the message `Invalid operation.`:
```javascript
try {
throw new errors.InvalidOperation();
} catch (ex) {
console.log(ex.message);
// => 'Invalid operation.'
}
```
If you want to specify a custom message, feel free to do so:
```javascript
throw new errors.InvalidOperation('Something failed.');
```
Additionally, if an error was caused by another error, you can specify this error as the second parameter.
Additionally, if an error was caused by another error, you can specify this error as the second parameter:

@@ -59,3 +70,3 @@ ```javascript

From time to time, you may want to provide custom error codes. For that specify an object with a `name` and a `code` property instead of only providing the error name.
From time to time, you may want to provide custom error codes. For that specify an object with a `name` and a `code` property instead of only providing the error name:

@@ -74,6 +85,6 @@ ```javascript

To build this module use [roboter](https://www.npmjs.com/package/roboter).
To build this module use [roboter](https://www.npmjs.com/package/roboter):
```shell
$ bot
$ npx roboter
```

@@ -80,0 +91,0 @@

@@ -7,24 +7,21 @@ 'use strict';

const defekt = require('../../lib/defekt');
const defekt = require('../../src/defekt');
suite('defekt', () => {
test('is a function.', done => {
test('is a function.', async () => {
assert.that(defekt).is.ofType('function');
done();
});
test('throws an error if error names are missing.', done => {
test('throws an error if error names are missing.', async () => {
assert.that(() => {
defekt();
}).is.throwing('Error names are missing.');
done();
});
test('returns an object.', done => {
test('returns an object.', async () => {
assert.that(defekt([ 'InvalidOperation' ])).is.ofType('object');
done();
});
suite('errors', () => {
test('contains the specified errors.', done => {
test('contains the specified errors.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -34,7 +31,6 @@

assert.that(errors.ArgumentNull).is.ofType('function');
done();
});
suite('CustomError', () => {
test('is an error.', done => {
test('is an error.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -44,6 +40,5 @@ const error = new errors.InvalidOperation();

assert.that(error).is.instanceOf(Error);
done();
});
test('is recognized by util.isError.', done => {
test('is recognized by util.isError.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -53,7 +48,6 @@ const error = new errors.InvalidOperation();

assert.that(util.isError(error)).is.true();
done();
});
suite('name', () => {
test('contains the given name.', done => {
test('contains the given name.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -63,3 +57,2 @@ const error = new errors.InvalidOperation();

assert.that(error.name).is.equalTo('InvalidOperation');
done();
});

@@ -69,3 +62,3 @@ });

suite('code', () => {
test('is the E-prefixed upper-cased name by default.', done => {
test('is the E-prefixed upper-cased name by default.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -75,6 +68,5 @@ const error = new errors.InvalidOperation();

assert.that(error.code).is.equalTo('EINVALIDOPERATION');
done();
});
test('is set to the given value.', done => {
test('is set to the given value.', async () => {
const errors = defekt([

@@ -87,3 +79,2 @@ { name: 'InvalidOperation', code: 'INVOP' },

assert.that(error.code).is.equalTo('INVOP');
done();
});

@@ -93,11 +84,10 @@ });

suite('message', () => {
test('contains an empty string if no message was given.', done => {
test('contains the human readable error name if no message was given.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);
const error = new errors.InvalidOperation();
assert.that(error.message).is.equalTo('');
done();
assert.that(error.message).is.equalTo('Invalid operation.');
});
test('contains the given message.', done => {
test('contains the given message.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -107,3 +97,2 @@ const error = new errors.InvalidOperation('foobar');

assert.that(error.message).is.equalTo('foobar');
done();
});

@@ -113,3 +102,3 @@ });

suite('cause', () => {
test('is undefined if no inner error is given.', done => {
test('is undefined if no inner error is given.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -119,6 +108,5 @@ const error = new errors.InvalidOperation('foobar');

assert.that(error.cause).is.undefined();
done();
});
test('contains the given inner error.', done => {
test('contains the given inner error.', async () => {
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]);

@@ -129,3 +117,2 @@ const cause = new errors.ArgumentNull();

assert.that(error.cause).is.equalTo(cause);
done();
});

@@ -132,0 +119,0 @@ });

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