mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 15:25:07 +01:00
Improve error messages for invalid resize parameters
Dependency version bumps and doc refresh
This commit is contained in:
18
lib/is.js
18
lib/is.js
@@ -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
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user