Improve error messages for invalid resize parameters

Dependency version bumps and doc refresh
This commit is contained in:
Lovell Fuller
2017-01-05 22:16:59 +00:00
parent 4858ebe051
commit 70a3d4fb5e
15 changed files with 182 additions and 76 deletions

View File

@@ -80,6 +80,21 @@ const inArray = function (val, list) {
return list.indexOf(val) !== -1;
};
/**
* Create an Error with a message relating to an invalid parameter.
*
* @param {String} name - parameter name.
* @param {String} expected - description of the type/value/range expected.
* @param {*} actual - the value received.
* @returns {Error} Containing the formatted message.
* @private
*/
const invalidParameterError = function (name, expected, actual) {
return new Error(
`Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
);
};
module.exports = {
defined: defined,
object: object,
@@ -90,5 +105,6 @@ module.exports = {
number: number,
integer: integer,
inRange: inRange,
inArray: inArray
inArray: inArray,
invalidParameterError: invalidParameterError
};

View File

@@ -104,7 +104,7 @@ const resize = function resize (width, height, options) {
if (is.integer(width) && is.inRange(width, 1, this.constructor.maximum.width)) {
this.options.width = width;
} else {
throw new Error('Invalid width (1 to ' + this.constructor.maximum.width + ') ' + width);
throw is.invalidParameterError('width', `integer between 1 and ${this.constructor.maximum.width}`, width);
}
} else {
this.options.width = -1;
@@ -113,7 +113,7 @@ const resize = function resize (width, height, options) {
if (is.integer(height) && is.inRange(height, 1, this.constructor.maximum.height)) {
this.options.height = height;
} else {
throw new Error('Invalid height (1 to ' + this.constructor.maximum.height + ') ' + height);
throw is.invalidParameterError('height', `integer between 1 and ${this.constructor.maximum.height}`, height);
}
} else {
this.options.height = -1;
@@ -124,7 +124,7 @@ const resize = function resize (width, height, options) {
if (is.string(kernel[options.kernel])) {
this.options.kernel = kernel[options.kernel];
} else {
throw new Error('Invalid kernel ' + options.kernel);
throw is.invalidParameterError('kernel', 'valid kernel name', options.kernel);
}
}
// Interpolator
@@ -132,7 +132,7 @@ const resize = function resize (width, height, options) {
if (is.string(interpolator[options.interpolator])) {
this.options.interpolator = interpolator[options.interpolator];
} else {
throw new Error('Invalid interpolator ' + options.interpolator);
throw is.invalidParameterError('interpolator', 'valid interpolator name', options.interpolator);
}
}
// Centre sampling
@@ -185,7 +185,7 @@ const crop = function crop (crop) {
// Strategy
this.options.crop = crop;
} else {
throw new Error('Unsupported crop ' + crop);
throw is.invalidParameterError('crop', 'valid crop id/name/strategy', crop);
}
return this;
};