From 7017af303d23b6bfa099479b7b3488756880fd24 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 8 Feb 2021 11:46:13 +0000 Subject: [PATCH] Improve error message when attempting toFile/GIF without magick --- lib/output.js | 32 ++++++++++++++++---------------- test/unit/gif.js | 15 ++++++++++----- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/output.js b/lib/output.js index 5ffbfa11..2abe60b4 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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); diff --git a/test/unit/gif.js b/test/unit/gif.js index c13f42e3..ff59be28 100644 --- a/test/unit/gif.js +++ b/test/unit/gif.js @@ -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/ ); }); }