diff --git a/docs/changelog.md b/docs/changelog.md index 67fe65ce..f4f3d47a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -17,6 +17,9 @@ Requires libvips v8.7.0. * Expose `pages` and `pageHeight` metadata for multi-page input images. [#1205](https://github.com/lovell/sharp/issues/1205) +* Expose underlying error message for invalid input. + [#1505](https://github.com/lovell/sharp/issues/1505) + * Prevent mutatation of options passed to `jpeg`. [#1516](https://github.com/lovell/sharp/issues/1516) diff --git a/package.json b/package.json index 0da7a181..3b3bf661 100644 --- a/package.json +++ b/package.json @@ -112,7 +112,7 @@ "documentation": "^9.1.1", "exif-reader": "^1.0.2", "icc": "^1.0.0", - "license-checker": "^24.1.0", + "license-checker": "^25.0.1", "mocha": "^5.2.0", "mock-fs": "^4.7.0", "nyc": "^13.1.0", diff --git a/src/common.cc b/src/common.cc index ca8f10ac..c4b301b6 100644 --- a/src/common.cc +++ b/src/common.cc @@ -245,8 +245,8 @@ namespace sharp { if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) { SetDensity(image, descriptor->density); } - } catch (...) { - throw vips::VError("Input buffer has corrupt header"); + } catch (vips::VError const &err) { + throw vips::VError(std::string("Input buffer has corrupt header: ") + err.what()); } } else { throw vips::VError("Input buffer contains unsupported image format"); @@ -287,8 +287,8 @@ namespace sharp { if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) { SetDensity(image, descriptor->density); } - } catch (...) { - throw vips::VError("Input file has corrupt header"); + } catch (vips::VError const &err) { + throw vips::VError(std::string("Input file has corrupt header: ") + err.what()); } } else { throw vips::VError("Input file is missing or of an unsupported image format"); diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 28e9a0b2..3b335065 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -70,6 +70,7 @@ module.exports = { inputJpgCenteredImage: getPath('centered_image.jpeg'), inputJpgRandom: getPath('random.jpg'), // convert -size 200x200 xc: +noise Random random.jpg inputJpgThRandom: getPath('thRandom.jpg'), // convert random.jpg -channel G -threshold 5% -separate +channel -negate thRandom.jpg + inputJpgLossless: getPath('testimgl.jpg'), // Lossless JPEG from ftp://ftp.fu-berlin.de/unix/X11/graphics/ImageMagick/delegates/ljpeg-6b.tar.gz inputPng: getPath('50020484-00001.png'), // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png inputPngWithTransparency: getPath('blackbug.png'), // public domain diff --git a/test/fixtures/testimgl.jpg b/test/fixtures/testimgl.jpg new file mode 100644 index 00000000..82b6b036 Binary files /dev/null and b/test/fixtures/testimgl.jpg differ diff --git a/test/unit/metadata.js b/test/unit/metadata.js index fd07ab20..a8406e67 100644 --- a/test/unit/metadata.js +++ b/test/unit/metadata.js @@ -457,6 +457,7 @@ describe('Image metadata', function () { sharp(fixtures.inputJpgWithCorruptHeader) .metadata(function (err) { assert.strictEqual(true, !!err); + assert.strictEqual(true, /Input file has corrupt header: VipsJpeg: Premature end of JPEG file/.test(err.message)); done(); }); }); @@ -465,6 +466,16 @@ describe('Image metadata', function () { sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) .metadata(function (err) { assert.strictEqual(true, !!err); + assert.strictEqual(true, /Input buffer has corrupt header: VipsJpeg: Premature end of JPEG file/.test(err.message)); + done(); + }); + }); + + it('Unsupported lossless JPEG passes underlying error message', function (done) { + sharp(fixtures.inputJpgLossless) + .metadata(function (err) { + assert.strictEqual(true, !!err); + assert.strictEqual(true, /Input file has corrupt header: VipsJpeg: Unsupported JPEG process: SOF type 0xc3/.test(err.message)); done(); }); });