Skip to content

Commit

Permalink
Improve error message for missing file that might be SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Mar 2, 2022
1 parent 21a9607 commit af80d7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ namespace sharp {
// From filesystem
imageType = DetermineImageType(descriptor->file.data());
if (imageType == ImageType::MISSING) {
if (descriptor->file.find("<svg") != std::string::npos) {
throw vips::VError("Input file is missing, did you mean "
"sharp(Buffer.from('" + descriptor->file.substr(0, 8) + "...')?");
}
throw vips::VError("Input file is missing");
}
if (imageType != ImageType::UNKNOWN) {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,11 @@ describe('SVG input', function () {
assert.strictEqual(info.height, 240);
assert.strictEqual(info.channels, 4);
});

it('Detects SVG passed as a string', () =>
assert.rejects(
() => sharp('<svg></svg>').toBuffer(),
/Input file is missing, did you mean/
)
);
});

0 comments on commit af80d7e

Please sign in to comment.