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

View File

@ -63,12 +63,17 @@ describe('GIF input', () => {
); );
if (!sharp.format.magick.output.buffer) { 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( assert.throws(
() => { () => sharp().gif(),
sharp().gif(); /GIF output requires libvips with support for ImageMagick/
}, );
/The gif operation requires libvips to have been installed 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/
); );
}); });
} }