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
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ gulp.task('release', ['bump'], function (done) {
done()
return
}
var pkg = require('./package.json')
var pkg = require('./package')
var version = 'v' + pkg.version
var releaseType = getBumpType()
var commitMsg = 'Releasing ' + releaseType + ' version: ' + version
Expand Down
2 changes: 2 additions & 0 deletions lib/ConnectSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ function appendIf (filter, middleware) {
this.append(function (req, res, next) {
if (filter(req)) {
middleware(req, res, next)
return
} else {
next()
return
}
})
}
54 changes: 54 additions & 0 deletions lib/errors/CustomError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict'

// var PrivateMethodError = require('./PrivateMethodError')
// var ProtectedMethodError = require('./ProtectedMethodError')

module.exports = CustomError

CustomError.prototype = Object.create(Error.prototype)
CustomError.prototype.constructor = CustomError
CustomError.prototype.setMessage = setMessage
CustomError.prototype.createStackTrace = createStackTrace

/**
* CustomError
* @class
* @mixin
* @property {String} message The error message
* @property {String} stack The error stack trace
*
* @constructor
* @param {String} [msg] The error message
*/
function CustomError (msg) {
// Error.call(this, msg)
this.setMessage(msg)
this.createStackTrace()
}

/**
* Set the error message from the given argument or from the class property DEFAULT_ERROR_MESSAGE
* @method
*/
function setMessage (msg) {
if (msg && typeof msg === 'string') {
this.message = msg
} else if (this.constructor.name !== 'CustomError') {
this.message = this.constructor.DEFAULT_ERROR_MESSAGE
} else {
this.message = undefined
}
}

/**
* Set the the correct stack trace for this error
* @method
* @returns {undefined}
* @throws {PrivateMethodError}
*/
function createStackTrace () {
var stack = new Error().stack
var splited = stack.split('\n')
var modifiedStack = splited[0].concat('\n', splited.splice(3).join('\n'))
this.stack = modifiedStack
}
71 changes: 7 additions & 64 deletions lib/errors/MissingArgumentError.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict'

var PrivateMethodError = require('./PrivateMethodError')
var CustomError = require('./CustomError')

module.exports = MissingArgumentError

MissingArgumentError.DEFAULT_ERROR_MESSAGE = 'One or more argument are missing'

MissingArgumentError.prototype = Object.create(CustomError.prototype)
MissingArgumentError.prototype.constructor = MissingArgumentError

/**
* MissingArgumentError
* @class
Expand All @@ -14,67 +19,5 @@ module.exports = MissingArgumentError
* @param {String} [msg] The error message
*/
function MissingArgumentError (msg) {
// Error.call(this, msg)
if (msg && typeof msg === 'string') {
this.message = msg
} else {
this.message = MissingArgumentError.DEFAULT_ERROR_MESSAGE
}
this.createStackTrace()
}

MissingArgumentError.DEFAULT_ERROR_MESSAGE = 'One or more argument are missing'

MissingArgumentError.prototype = Object.create(Error.prototype)
MissingArgumentError.prototype.constructor = MissingArgumentError
MissingArgumentError.prototype.createStackTrace = createStackTrace

/**
* Set the the correct stack trace for this error
* @method
* @private
* @returns {undefined}
* @throws {PrivateMethodError}
*/
function createStackTrace () {
privatize()
var stack = new Error().stack
var splited = stack.split('\n')
var modifiedStack = splited[0].concat('\n', splited.splice(3).join('\n'))
this.stack = modifiedStack
}

/**
* Throws a PrivateMethodError if the third level of a new stack trace differs of the first
* @function
* @inner
* @returns {undefined}
* @throws {PrivateMethodError}
*/
function privatize () {
var trace = new Error().stack
var here = getFileCall(trace, 1)
var caller = getFileCall(trace, 3)
if (here !== caller) {
throw new PrivateMethodError()
}
}

/**
* Get the file path of the caller at a given stack level
* @function
* @inner
* @param {String} trace A given stack trace
* @param {Number} level A given stack level to get the file path at this level
* @returns {String} The file path for this level
*/
function getFileCall (trace, level) {
var firstLineOfStack = trace.split('\n')[level]
var splitted = firstLineOfStack.split('(')
firstLineOfStack = splitted.splice(1).join('')
splitted = firstLineOfStack.split(')')
firstLineOfStack = splitted.join('')
splitted = firstLineOfStack.split(':')
firstLineOfStack = splitted.slice(0, -2).join('')
return firstLineOfStack
CustomError.call(this, msg)
}
64 changes: 5 additions & 59 deletions lib/errors/PrivateMethodError.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

var CustomError = require('./CustomError')

module.exports = PrivateMethodError

PrivateMethodError.DEFAULT_ERROR_MESSAGE = 'This is a private method'

PrivateMethodError.prototype = Object.create(Error.prototype)
PrivateMethodError.prototype = Object.create(CustomError.prototype)
PrivateMethodError.prototype.constructor = PrivateMethodError
PrivateMethodError.prototype.createStackTrace = createStackTrace

/**
* PrivateMethodError
Expand All @@ -15,63 +16,8 @@ PrivateMethodError.prototype.createStackTrace = createStackTrace
* @property {String} stack The error stack trace
*
* @constructor
* @param {String} msg The error message
* @param {String} msg A custom error message
*/
function PrivateMethodError (msg) {
if (msg && typeof msg === 'string') {
this.message = msg
} else {
this.message = PrivateMethodError.DEFAULT_ERROR_MESSAGE
}
this.createStackTrace()
}

/**
* Set the the correct stack trace for this error
* @method
* @private
* @returns {undefined}
* @throws {PrivateMethodError}
*/
function createStackTrace () {
privatize()
var stack = new Error().stack
var splited = stack.split('\n')
var modifiedStack = splited[0].concat('\n', splited.splice(3).join('\n'))
this.stack = modifiedStack
}

/**
* Throws a PrivateMethodError if the third level of a new stack trace differs of the first
* @function
* @inner
* @returns {undefined}
* @throws {PrivateMethodError}
*/
function privatize () {
var trace = new Error().stack
var here = getFileCall(trace, 1)
var caller = getFileCall(trace, 3)
if (here !== caller) {
throw new PrivateMethodError()
}
}

/**
* Get the file path of the caller at a given stack level
* @function
* @inner
* @param {String} trace A given stack trace
* @param {Number} level A given stack level to get the file path at this level
* @returns {String} The file path for this level
*/
function getFileCall (trace, level) {
var firstLineOfStack = trace.split('\n')[level]
var splitted = firstLineOfStack.split('(')
firstLineOfStack = splitted.splice(1).join('')
splitted = firstLineOfStack.split(')')
firstLineOfStack = splitted.join('')
splitted = firstLineOfStack.split(':')
firstLineOfStack = splitted.slice(0, -2).join('')
return firstLineOfStack
CustomError.call(this, msg)
}
103 changes: 103 additions & 0 deletions tests/errors/CustomError.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'use strict'

var path = require('path')
var chai = require('chai')

var CustomError = require(path.resolve('./lib/errors/CustomError'))

var describe = global.describe
var it = global.it
var expect = chai.expect

describe('CustomError', function () {
it('should be a function', function () {
expect(CustomError).to.be.a('function')
})

it("should not have a 'DEFAULT_ERROR_MESSAGE' static constant property (child classes should)", function () {
expect(CustomError).to.not.have.a.property('DEFAULT_ERROR_MESSAGE')
})

describe('.prototype', function () {
it('should be a function', function () {
expect(CustomError.prototype).to.be.a('OBJECT')
})

it('should be an instance of Error', function () {
expect(CustomError.prototype).to.be.an.instanceof(Error)
})

describe('.constructor()', function () {
it('should be an instance of CustomError', function () {
var err = new CustomError()
expect(CustomError.prototype).to.be.an.instanceof(Error)
expect(err).to.be.an.instanceof(CustomError)
})

it("should have a 'name' property of type 'String'", function () {
var msg = "'arg' is missing"
var err = new CustomError(msg)
expect(err).to.be.a.property('name')
expect(err.message).to.be.a('String')
})

it("should have a 'message' property of type 'String'", function () {
var msg = "'arg' is missing"
var err = new CustomError(msg)
expect(err).to.be.a.property('message')
expect(err.message).to.be.a('String')
expect(err.message).to.equal(msg)
})

it("should have a 'stack' property of type 'String'", function () {
var msg = "'arg' is missing"
var err = new CustomError(msg)
expect(err).to.be.a.property('stack')
expect(err.stack).to.be.a('String')
})

it("should have a 'createStackTrace' property of type 'function'", function () {
var err = new CustomError('yo')
expect(err).to.have.a.property('createStackTrace')
expect(err.createStackTrace).to.be.a('function')
})

describe('when giving a non-string argument', function () {
it('should not fail', function () {
var func = function () { return new CustomError(func) }
var obj = function () { return new CustomError({ foo: 'bar' }) }
var arr = function () { return new CustomError([ 'foo', 'baz' ]) }

expect(func).to.not.throw()
expect(obj).to.not.throw()
expect(arr).to.not.throw()
})
})

describe('when called without argument', function () {
it('should not have a default error message (child classes should)', function () {
var err = new CustomError()
expect(err.message).to.not.be.a('String')
expect(err.message).to.be.undefined
})
})

describe('when called without argument from a child class', function () {
it('should set the child class DEFAULT_ERROR_MESSAGE', function () {
var err
var ChildError = function ChildError (msg) {
CustomError.call(this, msg)
}
ChildError.prototype = Object.create(CustomError.prototype)
ChildError.prototype.constructor = ChildError
ChildError.DEFAULT_ERROR_MESSAGE = 'child error'
err = new ChildError()
expect(err).to.be.an('object')
expect(err).to.be.an.instanceof(ChildError)
expect(err.message).to.be.a('string')
expect(err.message).to.equal(ChildError.DEFAULT_ERROR_MESSAGE)
})
})
})
})
})
Loading