Split file-based input errors into missing vs invalid #1542

This commit is contained in:
Lovell Fuller 2019-01-19 11:59:36 +00:00
parent fa69ff773a
commit d5e98bc8ad
5 changed files with 36 additions and 7 deletions

View File

@ -8,6 +8,9 @@ Requires libvips v8.7.0.
* Input image decoding now fails fast, set `failOnError` to change this behaviour. * Input image decoding now fails fast, set `failOnError` to change this behaviour.
* Failed filesystem-based input now separates missing file and invalid format errors.
[#1542](https://github.com/lovell/sharp/issues/1542)
#### v0.21.2 - 13<sup>th</sup> January 2019 #### v0.21.2 - 13<sup>th</sup> January 2019
* Ensure all metadata is removed from PNG output unless `withMetadata` used. * Ensure all metadata is removed from PNG output unless `withMetadata` used.

View File

@ -32,7 +32,7 @@ const sharp = require('../build/Release/sharp.node');
*/ */
function toFile (fileOut, callback) { function toFile (fileOut, callback) {
if (!fileOut || fileOut.length === 0) { if (!fileOut || fileOut.length === 0) {
const errOutputInvalid = new Error('Invalid output'); const errOutputInvalid = new Error('Missing output file path');
if (is.fn(callback)) { if (is.fn(callback)) {
callback(errOutputInvalid); callback(errOutputInvalid);
} else { } else {

View File

@ -137,6 +137,7 @@ namespace sharp {
case ImageType::VIPS: id = "v"; break; case ImageType::VIPS: id = "v"; break;
case ImageType::RAW: id = "raw"; break; case ImageType::RAW: id = "raw"; break;
case ImageType::UNKNOWN: id = "unknown"; break; case ImageType::UNKNOWN: id = "unknown"; break;
case ImageType::MISSING: id = "missing"; break;
} }
return id; return id;
} }
@ -203,6 +204,10 @@ namespace sharp {
} else if (EndsWith(loader, "Magick") || EndsWith(loader, "MagickFile")) { } else if (EndsWith(loader, "Magick") || EndsWith(loader, "MagickFile")) {
imageType = ImageType::MAGICK; imageType = ImageType::MAGICK;
} }
} else {
if (EndsWith(vips::VError().what(), " not found\n")) {
imageType = ImageType::MISSING;
}
} }
return imageType; return imageType;
} }
@ -269,6 +274,9 @@ namespace sharp {
} else { } else {
// From filesystem // From filesystem
imageType = DetermineImageType(descriptor->file.data()); imageType = DetermineImageType(descriptor->file.data());
if (imageType == ImageType::MISSING) {
throw vips::VError("Input file is missing");
}
if (imageType != ImageType::UNKNOWN) { if (imageType != ImageType::UNKNOWN) {
try { try {
vips::VOption *option = VImage::option() vips::VOption *option = VImage::option()
@ -291,7 +299,7 @@ namespace sharp {
throw vips::VError(std::string("Input file has corrupt header: ") + err.what()); throw vips::VError(std::string("Input file has corrupt header: ") + err.what());
} }
} else { } else {
throw vips::VError("Input file is missing or of an unsupported image format"); throw vips::VError("Input file contains unsupported image format");
} }
} }
} }

View File

@ -106,7 +106,8 @@ namespace sharp {
FITS, FITS,
VIPS, VIPS,
RAW, RAW,
UNKNOWN UNKNOWN,
MISSING
}; };
// How many tasks are in the queue? // How many tasks are in the queue?

View File

@ -263,7 +263,8 @@ describe('Input/output', function () {
it('Fail when output File is input File', function (done) { it('Fail when output File is input File', function (done) {
sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) { sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) {
assert(!!err); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
@ -273,14 +274,16 @@ describe('Input/output', function () {
assert(false); assert(false);
done(); done();
}).catch(function (err) { }).catch(function (err) {
assert(!!err); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is empty', function (done) { it('Fail when output File is empty', function (done) {
sharp(fixtures.inputJpg).toFile('', function (err) { sharp(fixtures.inputJpg).toFile('', function (err) {
assert(!!err); assert(err instanceof Error);
assert.strictEqual('Missing output file path', err.message);
done(); done();
}); });
}); });
@ -290,7 +293,8 @@ describe('Input/output', function () {
assert(false); assert(false);
done(); done();
}).catch(function (err) { }).catch(function (err) {
assert(!!err); assert(err instanceof Error);
assert.strictEqual('Missing output file path', err.message);
done(); done();
}); });
}); });
@ -301,6 +305,7 @@ describe('Input/output', function () {
done(); done();
}).catch(function (err) { }).catch(function (err) {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Input buffer contains unsupported image format', err.message);
done(); done();
}); });
}); });
@ -311,6 +316,18 @@ describe('Input/output', function () {
done(); done();
}).catch(function (err) { }).catch(function (err) {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Input buffer contains unsupported image format', err.message);
done();
});
});
it('Fail when input file path is missing', function (done) {
sharp('does-not-exist').toBuffer().then(function () {
assert(false);
done();
}).catch(function (err) {
assert(err instanceof Error);
assert.strictEqual('Input file is missing', err.message);
done(); done();
}); });
}); });