-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Dakuan/db/more-options
options
- Loading branch information
Showing
8 changed files
with
117 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,37 @@ | ||
var R = require('ramda'), | ||
runOnProp = require('../util/run-on-prop'); | ||
randomElement = require('../util/random-element'), | ||
runOnProp = require('../util/run-on-prop'); | ||
|
||
var clientErrors = R.range(400, 418); | ||
var serverErrors = R.range(500, 506); | ||
var all = R.concat(clientErrors, serverErrors); | ||
|
||
var codeMatchingRegex = function(regex) { | ||
return R.find(function(c) { | ||
return R.func('test', regex, c); | ||
}, all); | ||
}; | ||
|
||
var parseOpt = R.cond( | ||
[R.is(Number), R.I], // if number then that pick that error code | ||
[R.is(Array), randomElement], // if array pick from those | ||
[R.is(RegExp), codeMatchingRegex], // if regex then code matching that regex | ||
[R.alwaysTrue, function() { | ||
return randomElement.call(null, all); | ||
}] // random error code | ||
); | ||
|
||
// sends an error code | ||
function _error(code) { | ||
var code = code || 500; | ||
return function _errorHandler(req, res, next) { | ||
console.log('CHAOS: throwing ' + code); | ||
res.status(code); | ||
return function(req, res, next) { | ||
var toThrow = parseOpt(code); | ||
console.log('CHAOS: throwing ' + toThrow); | ||
res.status(toThrow); | ||
res.end(); | ||
} | ||
} | ||
|
||
module.exports = { | ||
predicate: R.has('error'), | ||
factory: runOnProp(_error, 'error') | ||
}; | ||
predicate: R.has('error'), | ||
factory: runOnProp(_error, 'error') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// picks random element from an array | ||
function _randomElement(array) { | ||
return array[Math.floor(Math.random() * array.length)]; | ||
} | ||
|
||
module.exports = _randomElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var subject = require('../../src/handlers/error'), | ||
R = require('ramda'), | ||
assert = require('assert'); | ||
|
||
function assertStatus(handler, expectedCode) { | ||
handler(null, { | ||
status: function(code) { | ||
assert(code === expectedCode); | ||
}, | ||
end: function() {} | ||
}); | ||
} | ||
|
||
describe("Error handler", function() { | ||
describe("when a valid number is provided ", function() { | ||
it("should use that error code", function() { | ||
var handler = subject.factory({ | ||
error: 420 | ||
}); | ||
assertStatus(handler, 420); | ||
}); | ||
}); | ||
|
||
describe("when an array is provided", function() { | ||
it("should use a value from the array", function() { | ||
var handler = subject.factory({ | ||
error: [123] | ||
}); | ||
assertStatus(handler, 123); | ||
}); | ||
}); | ||
|
||
describe("when a regex is provided", function() { | ||
it("should use a value that matches the regex", function() { | ||
var handler = subject.factory({ | ||
error: /^400/ | ||
}); | ||
assertStatus(handler, 400); | ||
}); | ||
}); | ||
|
||
describe("when nothing is provided", function() { | ||
it("should use a valid error code", function() { | ||
var handler = subject.factory(); | ||
handler(null, { | ||
status: function(code) { | ||
assert(code >= 400); | ||
assert(code <= 506); | ||
assert(R.is(Number, code)); | ||
}, | ||
end: function() {} | ||
}); | ||
}); | ||
}); | ||
}); |