express-statuses
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "express-statuses", | ||
"description": "Simple response statuses for Express 4.x", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Pier Fumagalli", |
@@ -8,39 +8,64 @@ 'use strict'; | ||
function maker(code, message) { | ||
var made = function(object) { | ||
var status = { status: code, message: message }; | ||
var string = ''; | ||
function Status(object) { | ||
if (! (this instanceof Status)) return new Status(object); | ||
var msg = message, det, err, str = message; | ||
if (object) { | ||
if (typeof(object) === 'string') { | ||
status.message = object; | ||
string = ': ' + object; | ||
msg = object; | ||
str += ': ' + msg; | ||
} else if (object instanceof Error) { | ||
status.error = object; | ||
err = object; | ||
} else { | ||
if (object.message) { | ||
status.message = object.message; | ||
string = ': ' + object; | ||
msg = object.message; | ||
str += ': ' + msg; | ||
} | ||
if (object.details) status.details = object.details; | ||
if (object.error) status.error = object.error; | ||
if (object.details) det = object.details; | ||
if (object.error) err = object.error; | ||
} | ||
} | ||
status.toString = function() { | ||
return this.status + " " + message + string; | ||
if (msg) this.message = msg; | ||
if (det) this.details = det; | ||
if (err) this.error = err; | ||
Object.defineProperty(this, 'toString', { | ||
enumerable: false, | ||
configurable: false, | ||
value: function() { | ||
return '[Status ' + this.status + ' ' + str + ']'; | ||
} | ||
}); | ||
Error.captureStackTrace(this, Status); | ||
if (err) { | ||
var stack = this.stack || this.toString(); | ||
if (err instanceof Error) { | ||
stack += '\n Caused by ' + (err.stack || err.toString()); | ||
} else { | ||
stack += '\n Caused by [' + typeof (err) + '] ' + err.toString(); | ||
} | ||
Object.defineProperty(this, 'stack', { | ||
enumerable: false, | ||
configurable: false, | ||
value: stack | ||
}); | ||
} | ||
return status; | ||
} | ||
made.status = code; | ||
made.message = message; | ||
made.toString = function() { | ||
return this.status + " " + this.message; | ||
Status.prototype = new Error(); | ||
Status.prototype.name = 'Status ' + code; | ||
Status.prototype.status = Status.status = code; | ||
Status.prototype.message = Status.message = message; | ||
Status.prototype.toString = Status.toString = function() { | ||
return '[Status ' + this.status + ' ' + this.message + ']'; | ||
} | ||
made.toJSON = function() { | ||
return { status: this.status, message: this.message } | ||
Status.prototype.toJSON = Status.toJSON = function() { | ||
var json = { status: this.status, message: this.message } | ||
if (this.details) json.details = JSON.parse(JSON.stringify(this.details)); | ||
return json; | ||
} | ||
return made; | ||
return Status; | ||
}; | ||
@@ -69,3 +94,3 @@ | ||
} else { | ||
return byCode[code] || maker(code, "Unknown"); | ||
return byCode[code] || maker(code, 'Unknown'); | ||
} | ||
@@ -72,0 +97,0 @@ }; |
@@ -23,8 +23,22 @@ var expect = require('chai').expect; | ||
it('should create with a status number', function() { | ||
var status = statuses(404); | ||
var status = statuses(400); | ||
expect(status).to.be.a('function'); | ||
expect(status.status).to.equal(404); | ||
expect(status.message).to.equal('Not Found'); | ||
expect(status.status).to.equal(400); | ||
expect(status.message).to.equal('Bad Request'); | ||
expect(status.toString()).to.equal('[Status 400 Bad Request]'); | ||
expect(status.toJSON()).to.eql({ | ||
status: 400, | ||
message: 'Bad Request' | ||
}) | ||
expect(status('Override')).to.eql({ status: 404, message: 'Override' }); | ||
var override = new status('Override'); | ||
expect(override).to.be.instanceof(Error); | ||
expect(override.stack).to.match(/^Status 400: Override$/m); | ||
expect(override.status).to.equal(400); | ||
expect(override.message).to.equal('Override'); | ||
expect(override.toString()).to.equal('[Status 400 Bad Request: Override]'); | ||
expect(override.toJSON()).to.eql({ | ||
status: 400, | ||
message: 'Override' | ||
}) | ||
}) | ||
@@ -37,4 +51,18 @@ | ||
expect(status.message).to.equal('Unknown'); | ||
expect(status.toString()).to.equal('[Status 543 Unknown]'); | ||
expect(status.toJSON()).to.eql({ | ||
status: 543, | ||
message: 'Unknown' | ||
}) | ||
expect(status('Override')).to.eql({ status: 543, message: 'Override' }); | ||
var override = new status('Override'); | ||
expect(override).to.be.instanceof(Error); | ||
expect(override.stack).to.match(/^Status 543: Override$/m); | ||
expect(override.status).to.equal(543); | ||
expect(override.message).to.equal('Override'); | ||
expect(override.toString()).to.equal('[Status 543 Unknown: Override]'); | ||
expect(override.toJSON()).to.eql({ | ||
status: 543, | ||
message: 'Override' | ||
}) | ||
}) | ||
@@ -47,4 +75,18 @@ | ||
expect(status.message).to.equal('Invalid status code (999)'); | ||
expect(status.toString()).to.equal('[Status 500 Invalid status code (999)]'); | ||
expect(status.toJSON()).to.eql({ | ||
status: 500, | ||
message: 'Invalid status code (999)' | ||
}) | ||
expect(status('Override')).to.eql({ status: 500, message: 'Override' }); | ||
var override = new status('Override'); | ||
expect(override).to.be.instanceof(Error); | ||
expect(override.stack).to.match(/^Status 500: Override$/m); | ||
expect(override.status).to.equal(500); | ||
expect(override.message).to.equal('Override'); | ||
expect(override.toString()).to.equal('[Status 500 Invalid status code (999): Override]'); | ||
expect(override.toJSON()).to.eql({ | ||
status: 500, | ||
message: 'Override' | ||
}) | ||
}) | ||
@@ -57,4 +99,18 @@ | ||
expect(status.message).to.equal('Status not numeric ("foobar")'); | ||
expect(status.toString()).to.equal('[Status 500 Status not numeric ("foobar")]'); | ||
expect(status.toJSON()).to.eql({ | ||
status: 500, | ||
message: 'Status not numeric ("foobar")' | ||
}) | ||
expect(status('Override')).to.eql({ status: 500, message: 'Override' }); | ||
var override = new status('Override'); | ||
expect(override).to.be.instanceof(Error); | ||
expect(override.stack).to.match(/^Status 500: Override$/m); | ||
expect(override.status).to.equal(500); | ||
expect(override.message).to.equal('Override'); | ||
expect(override.toString()).to.equal('[Status 500 Status not numeric ("foobar"): Override]'); | ||
expect(override.toJSON()).to.eql({ | ||
status: 500, | ||
message: 'Override' | ||
}) | ||
}) | ||
@@ -64,13 +120,21 @@ | ||
var error = new Error("The error message"); | ||
expect(statuses.INTERNAL_SERVER_ERROR(error)).to.eql({ | ||
var override = statuses.INTERNAL_SERVER_ERROR(error); | ||
expect(override).to.be.instanceof(Error); | ||
expect(override.stack).to.match(/^Status 500: Internal Server Error$/m); | ||
expect(override.stack).to.match(/^\s+Caused by Error: The error message$/m); | ||
expect(override.status).to.equal(500); | ||
expect(override.message).to.equal('Internal Server Error'); | ||
expect(override.toString()).to.equal('[Status 500 Internal Server Error]'); | ||
expect(override.toJSON()).to.eql({ | ||
status: 500, | ||
message: 'Internal Server Error', | ||
error: error | ||
}); | ||
message: 'Internal Server Error' | ||
}) | ||
}) | ||
it('should incorporate an object', function() { | ||
var error = new Error("The error message"); | ||
var error = new Error("The error message part deux"); | ||
var details = { key: 'Some random details' }; | ||
expect(statuses.INTERNAL_SERVER_ERROR({ | ||
var override = statuses.INTERNAL_SERVER_ERROR({ | ||
status: 200, // This will be ignored! | ||
@@ -81,7 +145,15 @@ message: 'Override message', | ||
junk: 12345 | ||
})).to.eql({ | ||
}); | ||
expect(override).to.be.instanceof(Error); | ||
expect(override.stack).to.match(/^Status 500: Override message$/m); | ||
expect(override.stack).to.match(/^\s+Caused by Error: The error message part deux$/m); | ||
expect(override.status).to.equal(500); | ||
expect(override.message).to.equal('Override message'); | ||
expect(override.toString()).to.equal('[Status 500 Internal Server Error: Override message]'); | ||
expect(override.toJSON()).to.eql({ | ||
status: 500, // This can not be overridden | ||
message: 'Override message', | ||
details: details, | ||
error: error | ||
}); | ||
@@ -88,0 +160,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15058
238