Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#
# Directories
#
NODE_MODULES := './node_modules'
ROOT := $(shell pwd)
NODE_MODULES := $(ROOT)/node_modules
NODE_BIN := $(NODE_MODULES)/.bin


Expand All @@ -22,11 +23,12 @@ NPM := npm
#
GIT_HOOK_SRC = '../../tools/githooks/pre-push'
GIT_HOOK_DEST = '.git/hooks/pre-push'
LIB_FILES = './lib'
TEST_FILES = './test'
COVERAGE_FILES = './coverage'
LCOV = './coverage/lcov.info'

LIB_FILES := $(ROOT)/lib
TEST_FILES := $(ROOT)/test
COVERAGE_FILES := $(ROOT)/coverage
LCOV := $(ROOT)/coverage/lcov.info
SRCS := $(shell find $(LIB_FILES) $(TEST_FILES) -name '*.js' -type f \
-not \( -path './node_modules/*' -prune \))

#
# Targets
Expand All @@ -48,18 +50,18 @@ githooks:


.PHONY: lint
lint: node_modules
$(ESLINT) $(LIB_FILES) $(TEST_FILES)
lint: node_modules $(ESLINT) $(SRCS)
$(ESLINT) $(SRCS)


.PHONY: codestyle
codestyle: node_modules
$(JSCS) $(LIB_FILES) $(TEST_FILES)
codestyle: node_modules $(JSCS) $(SRCS)
$(JSCS) $(SRCS)


.PHONY: codestyle-fix
codestyle-fix: node_modules
$(JSCS) $(LIB_FILES) $(TEST_FILES) --fix
codestyle-fix: node_modules $(JSCS) $(SRCS)
$(JSCS) $(SRCS) --fix


.PHONY: prepush
Expand All @@ -77,7 +79,7 @@ coverage: node_modules clean-coverage


.PHONY: report-coverage
report: coverage
report-coverage: coverage
@cat $(LCOV) | $(COVERALLS)


Expand All @@ -94,4 +96,4 @@ clean: clean-coverage
#
## Debug -- print out a a variable via `make print-FOO`
#
#print-% : ; @echo $* = $($*)
print-% : ; @echo $* = $($*)
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,15 @@ For more information about building rich errors, check out
### Subclassing Errors

You can also create your own Error subclasses by using the provided
`makeConstructor()` method:
`makeConstructor()` method. Making a new subclass will add the constructor to
the existing exports object:

```js
var ExecutionError = errors.makeConstructor('ExecutionError', {
errors.makeConstructor('ExecutionError', {
statusCode: 406,
failureType: 'motion'
});
var myErr = new ExecutionError('bad joystick input!');
var myErr = new errors.ExecutionError('bad joystick input!');

console.log(myErr instanceof ExecutionError);
// => true
Expand Down Expand Up @@ -300,13 +301,13 @@ prefer that over `options.message`.

### makeConstructor(name [, defaults])

Creates a custom Error constructor.
Creates a custom Error constructor, adds it to the existing exports object.

* `name` {String} - the name of your Error
* `defaults` {Object} - an object of default values that will added to the
prototype.

**Returns:** {Function} an Error constructor, inherits from RestError
**Returns:** {void}

### makeErrFromCode(name [, args...])

Expand Down
8 changes: 6 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function makeConstructor(name, defaults) {
assert.string(name, 'name');
assert.optionalObject(defaults, 'defaults');

// assert that this constructor doesn't already exist.
assert.equal(typeof module.exports[name], 'undefined',
'Constructor already exists!');

// dynamically create a constructor.
// must be anonymous fn.
var ErrCtor = function() {
Expand All @@ -67,8 +71,8 @@ function makeConstructor(name, defaults) {
// copy over defaults
_.assign(ErrCtor.prototype, defaults);

// return the ctor
return ErrCtor;
// store constructor on main exports
module.exports[name] = ErrCtor;
}


Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"main": "lib/index.js",
"description": "Collection of Error objects shared across restify components.",
"homepage": "",
"homepage": "http://www.restify.com",
"author": {
"name": "Alex Liu",
"email": "donutespresso@gmail.com"
Expand All @@ -15,7 +15,7 @@
],
"repository": {
"type": "git",
"url": ""
"url": "https://github.com/restify/errors.git"
},
"license": "MIT",
"files": [
Expand All @@ -25,15 +25,19 @@
"restify-errors",
"restify",
"errors",
"restify-clients"
"custom errors",
"inherit errors",
"http errors",
"http status code",
"rest errors"
],
"scripts": {
"test": "make test"
"test": "make test"
},
"devDependencies": {
"chai": "^3.0.0",
"coveralls": "^2.11.2",
"eslint": "^0.23.0",
"eslint": "^0.24.0",
"istanbul": "^0.3.15",
"jscs": "^1.13.1",
"mkdirp": "^0.5.1",
Expand Down
24 changes: 17 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ describe('restify-errors node module.', function() {
});

it('should have test file as first line of subclass error stack trace', function testStack5() {
var ExecutionError = restifyErrors.makeConstructor('ExecutionError');
var err = new ExecutionError('did not charge long enough');
restifyErrors.makeConstructor('ChargeError');
var err = new restifyErrors.ChargeError('did not charge long enough');
var stack = err.stack.split('\n');

assert.equal(_.includes(stack[0], 'ExecutionError: did not charge long enough'), true);
assert.equal(_.includes(stack[0], 'ChargeError: did not charge long enough'), true);
assert.equal(_.includes(stack[1], 'Context.testStack5'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
Expand Down Expand Up @@ -460,15 +460,15 @@ describe('restify-errors node module.', function() {
});
});

it('should create custom error using "make"', function() {
it('should create custom error using makeConstructor', function() {
var underlyingErr = new Error('underlying error!');
var ExecutionError = restifyErrors.makeConstructor('ExecutionError', {
restifyErrors.makeConstructor('ExecutionError', {
statusCode: 406,
failureType: 'motion'
});
var err = new ExecutionError(underlyingErr, 'bad joystick input');
var err = new restifyErrors.ExecutionError(underlyingErr, 'bad joystick input');

assert.equal(err instanceof ExecutionError, true);
assert.equal(err instanceof restifyErrors.ExecutionError, true);
assert.equal(err instanceof RestError, true);
assert.equal(err instanceof HttpError, true);
assert.equal(err instanceof WError, true);
Expand All @@ -482,6 +482,16 @@ describe('restify-errors node module.', function() {
assert.equal(err.we_cause, underlyingErr);
});

it('should throw when creating a constructor that already exists', function() {
assert.throws(function() {
restifyErrors.makeConstructor('ExecutionError');
}, 'Constructor already exists!');

assert.throws(function() {
restifyErrors.makeConstructor('InternalServerError');
}, 'Constructor already exists!');
});

it('should create an error from an http status code', function() {
var err = restifyErrors.makeErrFromCode(406, 'the horror');

Expand Down