Merge pull request #49 from jonathanong/strings-are-not-errors

fix error types, support promises on convenience methods
This commit is contained in:
Lovell Fuller 2014-06-04 10:35:06 +01:00
commit c9aa9c7723

View File

@ -27,10 +27,10 @@ var Sharp = function(input) {
if (input.length > 0) { if (input.length > 0) {
this.options.bufferIn = input; this.options.bufferIn = input;
} else { } else {
throw 'Buffer is empty'; throw new Error('Buffer is empty');
} }
} else { } else {
throw 'Unsupported input ' + typeof input; throw new Error('Unsupported input ' + typeof input);
} }
return this; return this;
}; };
@ -100,7 +100,7 @@ Sharp.prototype.quality = function(quality) {
if (!Number.isNaN(quality) && quality >= 1 && quality <= 100) { if (!Number.isNaN(quality) && quality >= 1 && quality <= 100) {
this.options.quality = quality; this.options.quality = quality;
} else { } else {
throw 'Invalid quality (1 to 100) ' + quality; throw new Error('Invalid quality (1 to 100) ' + quality);
} }
return this; return this;
}; };
@ -109,7 +109,7 @@ Sharp.prototype.compressionLevel = function(compressionLevel) {
if (!Number.isNaN(compressionLevel) && compressionLevel >= -1 && compressionLevel <= 9) { if (!Number.isNaN(compressionLevel) && compressionLevel >= -1 && compressionLevel <= 9) {
this.options.compressionLevel = compressionLevel; this.options.compressionLevel = compressionLevel;
} else { } else {
throw 'Invalid compressionLevel (-1 to 9) ' + compressionLevel; throw new Error('Invalid compressionLevel (-1 to 9) ' + compressionLevel);
} }
return this; return this;
}; };
@ -121,7 +121,7 @@ Sharp.prototype.resize = function(width, height) {
if (!Number.isNaN(width)) { if (!Number.isNaN(width)) {
this.options.width = width; this.options.width = width;
} else { } else {
throw 'Invalid width ' + width; throw new Error('Invalid width ' + width);
} }
} }
if (!height) { if (!height) {
@ -130,7 +130,7 @@ Sharp.prototype.resize = function(width, height) {
if (!Number.isNaN(height)) { if (!Number.isNaN(height)) {
this.options.height = height; this.options.height = height;
} else { } else {
throw 'Invalid height ' + height; throw new Error('Invalid height ' + height);
} }
} }
return this; return this;
@ -141,10 +141,20 @@ Sharp.prototype.resize = function(width, height) {
*/ */
Sharp.prototype.toFile = function(output, callback) { Sharp.prototype.toFile = function(output, callback) {
if (!output || output.length === 0) { if (!output || output.length === 0) {
callback('Invalid output'); var err = new Error('Invalid output');
if (typeof callback === 'function') {
callback(err);
} else {
return Promise.reject(err);
}
} else { } else {
if (this.options.fileIn === output) { if (this.options.fileIn === output) {
callback('Cannot use same file for input and output'); var err = new Error('Cannot use same file for input and output');
if (typeof callback === 'function') {
callback(err);
} else {
return Promise.reject(err);
}
} else { } else {
return this._sharp(output, callback); return this._sharp(output, callback);
} }