Skip to content

Commit f68bbca

Browse files
committedOct 9, 2015
ability to create error instances without new keyword
1 parent 77e3554 commit f68bbca

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed
 

‎lib/helpers/class-generator.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ module.exports = function generateErrorClass(name, options){
2626
Class.captureStackTrace(this, Class);
2727
};
2828

29-
var classGeneratorFn = new Function('classConstructor',
30-
"return function " + name + "(" + options.args.join(', ') + "){ classConstructor.apply(this, arguments); };"
31-
);
29+
var classGeneratorFn = new Function('classConstructor', [
30+
"return function ", name, "(", options.args.join(', '), "){",
31+
"if(!(this instanceof ", name, ")) {",
32+
"var instance = Object.create(", name, ".prototype);",
33+
"classConstructor.apply(instance, arguments);",
34+
"return instance;",
35+
"} else {",
36+
"classConstructor.apply(this, arguments);",
37+
"}",
38+
"};",
39+
].join(''));
3240
var Class = classGeneratorFn(classConstructor);
3341

3442
util.inherits(Class, options.extends);

‎lib/http-status.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ var util = require('util');
33
var STATUS_CODE_ATTRIBUTE_NAME = module.exports.STATUS_CODE_ATTRIBUTE_NAME = 'status';
44

55
var HttpStatusError = module.exports = function HttpStatusError(status_code, message) {
6+
if(!(this instanceof HttpStatusError)) {
7+
var instance = Object.create(HttpStatusError.prototype);
8+
HttpStatusError.apply(instance, arguments);
9+
return instance;
10+
}
11+
612
if(typeof message == 'number' && typeof status_code != 'number') {
713
//old interface, so swap.
814
var c = message;

‎tests/http-status.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var assert = require('assert');
2+
var HttpStatusError = require('../').HttpStatusError;
3+
4+
describe("HttpStatusError", function(){
5+
function performBasicAssertions(error){
6+
assert.equal(error.name, "HttpStatusError"), 'Its name is correct.';
7+
console.log(error, error.stack)
8+
assert.ok(new RegExp(error.name + ": " + error.message.replace(/\)/g, '\\)').replace(/\(/g, '\\(') + "\n(.*\n)+").test(error.stack), "Stack is good");
9+
assert.ok(error instanceof Error, Error, "It is an instanceof Error");
10+
}
11+
12+
it("should work with status code and message", function(){
13+
var error = new HttpStatusError(403, "You got a 403");
14+
assert.equal(error.message, "You got a 403");
15+
assert.equal(error.status, 403);
16+
assert.equal(error.status_code, 403);
17+
assert.equal(error.statusCode, 403);
18+
performBasicAssertions(error);
19+
});
20+
21+
it("should work with status code", function(){
22+
var error = new HttpStatusError(403);
23+
assert.equal(error.message, "(403) Forbidden!");
24+
assert.equal(error.status, 403);
25+
assert.equal(error.status_code, 403);
26+
assert.equal(error.statusCode, 403);
27+
performBasicAssertions(error);
28+
});
29+
30+
it("should work with status code and message without new", function(){
31+
var error = HttpStatusError(403, "You got a 403");
32+
assert.equal(error.message, "You got a 403");
33+
assert.equal(error.status, 403);
34+
assert.equal(error.status_code, 403);
35+
assert.equal(error.statusCode, 403);
36+
performBasicAssertions(error);
37+
});
38+
39+
it("should work with status code without new", function(){
40+
var error = HttpStatusError(403);
41+
assert.equal(error.message, "(403) Forbidden!");
42+
assert.equal(error.status, 403);
43+
assert.equal(error.status_code, 403);
44+
assert.equal(error.statusCode, 403);
45+
performBasicAssertions(error);
46+
});
47+
48+
49+
});

‎tests/support/index.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,32 @@ module.exports.testError = function testError(name, opts){
88
opts.extends = opts.extends || Error;
99
opts.full_name = opts.full_name || name;
1010

11+
function performAssertions(Err, error) {
12+
assert.equal(error.name, name), 'Its name is correct.';
13+
assert.equal(error.message, opts.message_to_assert);
14+
assert.ok(new RegExp(error.name + ": " + error.message + "\n(.*\n)+").test(error.stack), "Stack is good");
15+
assert.equal(Err.super_.name, opts.extends.name, "It is an instance of" + opts.extends.name);
16+
assert.ok(error instanceof Error, opts.extends, "It is an instanceof " + opts.extends.name);
17+
}
18+
1119
describe(name, function(){
20+
var Err = errors;
21+
(opts.full_name).split('.').forEach(function(dir){ Err = Err[dir]; });
22+
1223
it("should work", function(){
13-
var Err = errors;
14-
(opts.full_name).split('.').forEach(function(dir){ Err = Err[dir]; });
1524
assert.ok(Err, name + " exists.");
1625
var inner_error = new Error("inner error");
1726
var error = new Err(opts.message);
18-
assert.equal(error.name, name), 'Its name is correct.';
19-
assert.equal(error.message, opts.message_to_assert);
20-
assert.ok(new RegExp(error.name + ": " + error.message + "\n(.*\n)+").test(error.stack), "Stack is good");
21-
assert.equal(Err.super_.name, opts.extends.name, "It is an instance of" + opts.extends.name);
22-
assert.ok(error instanceof Error, opts.extends, "It is an instanceof " + opts.extends.name);
27+
28+
performAssertions(Err, error);
29+
});
30+
31+
it("should work without new", function(){
32+
assert.ok(Err, name + " exists.");
33+
var inner_error = new Error("inner error");
34+
var error = Err(opts.message);
35+
36+
performAssertions(Err, error);
2337
});
2438
});
2539
}

0 commit comments

Comments
 (0)
Please sign in to comment.