create-error-class
Advanced tools
Comparing version 1.0.0 to 2.0.0
28
index.js
'use strict'; | ||
var util = require('util'); | ||
var objectAssign = require('object-assign'); | ||
var inherits = require('inherits'); | ||
var captureStackTrace = require('capture-stack-trace'); | ||
module.exports = function createErrorClass(className, constructor) { | ||
module.exports = function createErrorClass(className, setup) { | ||
if (typeof className !== 'string') { | ||
@@ -11,21 +11,11 @@ throw new TypeError('Expected className to be a string'); | ||
constructor = constructor || function () {}; | ||
setup = setup || function () {}; | ||
function CustomError(message, props) { | ||
Error.captureStackTrace(this, this.constructor); | ||
this.name = className; | ||
/* jshint evil:true */ | ||
var ErrorClass = eval('(function ' + className + '() { captureStackTrace(this, this.constructor); setup.apply(this, arguments); })'); | ||
if (typeof message !== 'string') { | ||
props = message; | ||
message = undefined; | ||
} | ||
inherits(ErrorClass, Error); | ||
ErrorClass.prototype.name = className; | ||
this.message = message; | ||
objectAssign(this, props); | ||
constructor.call(this); | ||
} | ||
util.inherits(CustomError, Error); | ||
return CustomError; | ||
return ErrorClass; | ||
}; |
{ | ||
"name": "create-error-class", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Create Error classes", | ||
@@ -25,3 +25,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"object-assign": "^3.0.0" | ||
"capture-stack-trace": "^1.0.0", | ||
"inherits": "^2.0.1" | ||
}, | ||
@@ -28,0 +29,0 @@ "devDependencies": { |
@@ -18,4 +18,4 @@ # create-error-class [![Build Status](https://travis-ci.org/floatdrop/create-error-class.svg?branch=master)](https://travis-ci.org/floatdrop/create-error-class) | ||
var HTTPError = createErrorClass('HTTPError', function () { | ||
this.message = 'Status code is ' + this.statusCode; | ||
var HTTPError = createErrorClass('HTTPError', function (props) { | ||
this.message = 'Status code is ' + props.statusCode; | ||
}); | ||
@@ -29,5 +29,5 @@ | ||
### createErrorClass(className, [constructor]) | ||
### createErrorClass(className, [setup]) | ||
Return constructor of Errors with `className`. Constructor accepts optional message and props, that will be assigned to Error. | ||
Return constructor of Errors with `className`. | ||
@@ -41,6 +41,6 @@ #### className | ||
#### constructor | ||
#### setup | ||
Type: `function` | ||
Function, that will be called after each Error object is created from constructor with context of Error object. | ||
Setup function, that will be called after each Error object is created from constructor with context of Error object. | ||
@@ -47,0 +47,0 @@ ## License |
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
2010
2
3
14
1
+ Addedcapture-stack-trace@^1.0.0
+ Addedinherits@^2.0.1
+ Addedcapture-stack-trace@1.0.2(transitive)
+ Addedinherits@2.0.4(transitive)
- Removedobject-assign@^3.0.0
- Removedobject-assign@3.0.0(transitive)