Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Raise argument errors more correctly.

  • Loading branch information...
commit a330bc237bab56c915ae2c7e1d17f51e8e2c2780 1 parent b5ae226
@cweider authored
Showing with 14 additions and 8 deletions.
  1. +12 −6 itertools.js
  2. +2 −2 test/test.js
View
18 itertools.js
@@ -15,6 +15,11 @@ __StopIteration.prototype = new Error();
var _StopIteration = typeof StopIteration != 'undefined' ? StopIteration : __StopIteration;
+function ArgumentError() {
+ Error.apply(this, arguments);
+}
+ArgumentError.prototype = new Error();
+
function _arrayForArguments(args) {
return Array.prototype.slice.call(args);
}
@@ -309,18 +314,18 @@ function slice() {
stop = arguments[2];
step = arguments[3];
} else {
- throw new Error("Argument Error: Stop must be specified.");
+ throw new ArgumentError("Stop must be specified.");
}
var i = 1;
if (!(start > -1)) {
- throw new Error("Argument Error: Start, must be a non-negative integer.");
+ throw new ArgumentError("Start, must be a non-negative integer.");
}
if (stop && !(stop > -1)) {
- throw new Error("Argument Error: Step, if defined, must be a non-negative integer.");
+ throw new ArgumentError("Step, if defined, must be a non-negative integer.");
}
if (!(step > 0)) {
- throw new Error("Argument Error: Step, if defined, must be a positive integer.");
+ throw new ArgumentError("Step, if defined, must be a positive integer.");
}
var skip = function (n) {
@@ -398,7 +403,7 @@ function tee(iterable, n) {
if (n === undefined) {
n = 2;
} else if (!(n > 0)) {
- throw new Error("Argument Error: The number of tees, if specified must be a positive number.");
+ throw new ArgumentError("The number of tees, if specified must be a positive number.");
}
var getNextForIterator = function (i) {
@@ -534,7 +539,7 @@ function some(iterable, count) {
var iterator = makeIteratorOf(iterable);
if (!(count > 0)) {
- throw new Error("Argument Error: Count must be positive number.");
+ throw new ArgumentError("Count must be positive number.");
}
try {
@@ -580,6 +585,7 @@ function reduce(func, iterable, init) {
exports.Iterator = makeIteratorOf;
exports.Array = toArray;
exports.StopIteration = _StopIteration;
+exports.ArgumentError = ArgumentError;
exports.count = count;
exports.cycle = cycle;
View
4 test/test.js
@@ -120,12 +120,12 @@ describe("slice", function () {
it('should require positive step', function () {
assert.throws(function () {
itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 6, -1);
- }, Error);
+ }, itertools.ArgumentError);
});
it('should require positive range', function () {
assert.throws(function () {
itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, -1, 5);
- }, Error);
+ }, itertools.ArgumentError);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.