Improve error message when attempting toFile/GIF without magick

This commit is contained in:
Lovell Fuller 2021-02-08 11:46:13 +00:00
parent 0dc325daa4
commit 7017af303d
2 changed files with 26 additions and 21 deletions

View File

@ -16,6 +16,8 @@ const formats = new Map([
['gif', 'gif']
]);
const errMagickSave = new Error('GIF output requires libvips with support for ImageMagick');
/**
* Write output image data to a file.
*
@ -47,25 +49,23 @@ const formats = new Map([
* @throws {Error} Invalid parameters
*/
function toFile (fileOut, callback) {
if (!fileOut || fileOut.length === 0) {
const errOutputInvalid = new Error('Missing output file path');
let err;
if (!is.string(fileOut)) {
err = new Error('Missing output file path');
} else if (this.options.input.file === fileOut) {
err = new Error('Cannot use same file for input and output');
} else if (this.options.formatOut === 'input' && fileOut.toLowerCase().endsWith('.gif') && !this.constructor.format.magick.output.file) {
err = errMagickSave;
}
if (err) {
if (is.fn(callback)) {
callback(errOutputInvalid);
callback(err);
} else {
return Promise.reject(errOutputInvalid);
return Promise.reject(err);
}
} else {
if (this.options.input.file === fileOut) {
const errOutputIsInput = new Error('Cannot use same file for input and output');
if (is.fn(callback)) {
callback(errOutputIsInput);
} else {
return Promise.reject(errOutputIsInput);
}
} else {
this.options.fileOut = fileOut;
return this._pipeline(callback);
}
this.options.fileOut = fileOut;
return this._pipeline(callback);
}
return this;
}
@ -426,7 +426,7 @@ function webp (options) {
/* istanbul ignore next */
function gif (options) {
if (!this.constructor.format.magick.output.buffer) {
throw new Error('The gif operation requires libvips to have been installed with support for ImageMagick');
throw errMagickSave;
}
trySetAnimationOptions(options, this.options);
return this._updateFormatOut('gif', options);

View File

@ -63,12 +63,17 @@ describe('GIF input', () => {
);
if (!sharp.format.magick.output.buffer) {
it('GIF output should fail due to missing ImageMagick', () => {
it('GIF buffer output should fail due to missing ImageMagick', () => {
assert.throws(
() => {
sharp().gif();
},
/The gif operation requires libvips to have been installed with support for ImageMagick/
() => sharp().gif(),
/GIF output requires libvips with support for ImageMagick/
);
});
it('GIF file output should fail due to missing ImageMagick', () => {
assert.rejects(
async () => await sharp().toFile('test.gif'),
/GIF output requires libvips with support for ImageMagick/
);
});
}