Skip to content

Commit 17d4a68

Browse files
committed
WIP: Switch from custom VError to standard runtime_error
1 parent ed6b738 commit 17d4a68

File tree

6 files changed

+30
-34
lines changed

6 files changed

+30
-34
lines changed

src/common.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,11 @@ namespace sharp {
510510
option = GetOptionsForImageType(imageType, descriptor);
511511
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
512512
}
513-
} catch (vips::VError const &err) {
514-
throw vips::VError(std::string("Input buffer has corrupt header: ") + err.what());
513+
} catch (std::runtime_error const &err) {
514+
throw std::runtime_error(std::string("Input buffer has corrupt header: ") + err.what());
515515
}
516516
} else {
517-
throw vips::VError("Input buffer contains unsupported image format");
517+
throw std::runtime_error("Input buffer contains unsupported image format");
518518
}
519519
}
520520
} else {
@@ -585,10 +585,10 @@ namespace sharp {
585585
imageType = DetermineImageType(descriptor->file.data());
586586
if (imageType == ImageType::MISSING) {
587587
if (descriptor->file.find("<svg") != std::string::npos) {
588-
throw vips::VError("Input file is missing, did you mean "
588+
throw std::runtime_error("Input file is missing, did you mean "
589589
"sharp(Buffer.from('" + descriptor->file.substr(0, 8) + "...')?");
590590
}
591-
throw vips::VError("Input file is missing: " + descriptor->file);
591+
throw std::runtime_error("Input file is missing: " + descriptor->file);
592592
}
593593
if (imageType != ImageType::UNKNOWN) {
594594
try {
@@ -600,19 +600,19 @@ namespace sharp {
600600
option = GetOptionsForImageType(imageType, descriptor);
601601
image = VImage::new_from_file(descriptor->file.data(), option);
602602
}
603-
} catch (vips::VError const &err) {
604-
throw vips::VError(std::string("Input file has corrupt header: ") + err.what());
603+
} catch (std::runtime_error const &err) {
604+
throw std::runtime_error(std::string("Input file has corrupt header: ") + err.what());
605605
}
606606
} else {
607-
throw vips::VError("Input file contains unsupported image format");
607+
throw std::runtime_error("Input file contains unsupported image format");
608608
}
609609
}
610610
}
611611

612612
// Limit input images to a given number of pixels, where pixels = width * height
613613
if (descriptor->limitInputPixels > 0 &&
614614
static_cast<uint64_t>(image.width()) * image.height() > descriptor->limitInputPixels) {
615-
throw vips::VError("Input image exceeds pixel limit");
615+
throw std::runtime_error("Input image exceeds pixel limit");
616616
}
617617
return std::make_tuple(image, imageType);
618618
}
@@ -788,19 +788,19 @@ namespace sharp {
788788
: image.height();
789789
if (imageType == ImageType::JPEG) {
790790
if (image.width() > 65535 || height > 65535) {
791-
throw vips::VError("Processed image is too large for the JPEG format");
791+
throw std::runtime_error("Processed image is too large for the JPEG format");
792792
}
793793
} else if (imageType == ImageType::WEBP) {
794794
if (image.width() > 16383 || height > 16383) {
795-
throw vips::VError("Processed image is too large for the WebP format");
795+
throw std::runtime_error("Processed image is too large for the WebP format");
796796
}
797797
} else if (imageType == ImageType::GIF) {
798798
if (image.width() > 65535 || height > 65535) {
799-
throw vips::VError("Processed image is too large for the GIF format");
799+
throw std::runtime_error("Processed image is too large for the GIF format");
800800
}
801801
} else if (imageType == ImageType::HEIF) {
802802
if (image.width() > 16384 || height > 16384) {
803-
throw vips::VError("Processed image is too large for the HEIF format");
803+
throw std::runtime_error("Processed image is too large for the HEIF format");
804804
}
805805
}
806806
}

src/metadata.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MetadataWorker : public Napi::AsyncWorker {
3131
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
3232
try {
3333
std::tie(image, imageType) = OpenInput(baton->input);
34-
} catch (vips::VError const &err) {
34+
} catch (std::runtime_error const &err) {
3535
(baton->err).append(err.what());
3636
}
3737
if (imageType != sharp::ImageType::UNKNOWN) {

src/operations.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "./operations.h"
1515

1616
using vips::VImage;
17-
using vips::VError;
1817

1918
namespace sharp {
2019
/*
@@ -287,7 +286,7 @@ namespace sharp {
287286
*/
288287
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt, int const margin) {
289288
if (image.width() < 3 && image.height() < 3) {
290-
throw VError("Image to trim must be at least 3x3 pixels");
289+
throw std::runtime_error("Image to trim must be at least 3x3 pixels");
291290
}
292291
if (background.size() == 0) {
293292
// Top-left pixel provides the default background colour if none is given
@@ -361,7 +360,7 @@ namespace sharp {
361360
VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) {
362361
size_t const bands = static_cast<size_t>(image.bands());
363362
if (a.size() > bands) {
364-
throw VError("Band expansion using linear is unsupported");
363+
throw std::runtime_error("Band expansion using linear is unsupported");
365364
}
366365
bool const uchar = !Is16Bit(image.interpretation());
367366
if (image.has_alpha() && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) {

src/pipeline.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class PipelineWorker : public Napi::AsyncWorker {
274274
}
275275
sharp::SetDensity(image, baton->input->density);
276276
if (image.width() > 32767 || image.height() > 32767) {
277-
throw vips::VError("Input SVG image will exceed 32767x32767 pixel limit when scaled");
277+
throw std::runtime_error("Input SVG image will exceed 32767x32767 pixel limit when scaled");
278278
}
279279
} else if (inputImageType == sharp::ImageType::PDF) {
280280
if (baton->input->buffer != nullptr) {
@@ -290,7 +290,7 @@ class PipelineWorker : public Napi::AsyncWorker {
290290
}
291291
} else {
292292
if (inputImageType == sharp::ImageType::SVG && (image.width() > 32767 || image.height() > 32767)) {
293-
throw vips::VError("Input SVG image exceeds 32767x32767 pixel limit");
293+
throw std::runtime_error("Input SVG image exceeds 32767x32767 pixel limit");
294294
}
295295
}
296296
if (baton->input->autoOrient) {
@@ -675,7 +675,7 @@ class PipelineWorker : public Napi::AsyncWorker {
675675

676676
// Verify within current dimensions
677677
if (compositeImage.width() > image.width() || compositeImage.height() > image.height()) {
678-
throw vips::VError("Image to composite must have same dimensions or smaller");
678+
throw std::runtime_error("Image to composite must have same dimensions or smaller");
679679
}
680680
// Check if overlay is tiled
681681
if (composite->tile) {
@@ -1086,20 +1086,19 @@ class PipelineWorker : public Napi::AsyncWorker {
10861086
// Get raw image data
10871087
baton->bufferOut = static_cast<char*>(image.write_to_memory(&baton->bufferOutLength));
10881088
if (baton->bufferOut == nullptr) {
1089-
(baton->err).append("Could not allocate enough memory for raw output");
1090-
return Error();
1089+
throw std::runtime_error("Could not allocate enough memory for raw output");
10911090
}
10921091
baton->formatOut = "raw";
10931092
} else {
10941093
// Unsupported output format
1095-
(baton->err).append("Unsupported output format ");
1094+
auto unsupported = std::string("Unsupported output format ");
10961095
if (baton->formatOut == "input") {
1097-
(baton->err).append("when trying to match input format of ");
1098-
(baton->err).append(ImageTypeId(inputImageType));
1096+
unsupported.append("when trying to match input format of ");
1097+
unsupported.append(ImageTypeId(inputImageType));
10991098
} else {
1100-
(baton->err).append(baton->formatOut);
1099+
unsupported.append(baton->formatOut);
11011100
}
1102-
return Error();
1101+
throw std::runtime_error(unsupported);
11031102
}
11041103
} else {
11051104
// File output
@@ -1274,7 +1273,7 @@ class PipelineWorker : public Napi::AsyncWorker {
12741273
return Error();
12751274
}
12761275
}
1277-
} catch (vips::VError const &err) {
1276+
} catch (std::runtime_error const &err) {
12781277
char const *what = err.what();
12791278
if (what && what[0]) {
12801279
(baton->err).append(what);
@@ -1306,7 +1305,6 @@ class PipelineWorker : public Napi::AsyncWorker {
13061305
}
13071306
warning = sharp::VipsWarningPop();
13081307
}
1309-
13101308
if (baton->err.empty()) {
13111309
int width = baton->width;
13121310
int height = baton->height;
@@ -1407,7 +1405,7 @@ class PipelineWorker : public Napi::AsyncWorker {
14071405

14081406
void MultiPageUnsupported(int const pages, std::string op) {
14091407
if (pages > 1) {
1410-
throw vips::VError(op + " is not supported for multi-page images");
1408+
throw std::runtime_error(op + " is not supported for multi-page images");
14111409
}
14121410
}
14131411

src/stats.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class StatsWorker : public Napi::AsyncWorker {
3939
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
4040
try {
4141
std::tie(image, imageType) = OpenInput(baton->input);
42-
} catch (vips::VError const &err) {
42+
} catch (std::runtime_error const &err) {
4343
(baton->err).append(err.what());
4444
}
4545
if (imageType != sharp::ImageType::UNKNOWN) {
@@ -92,7 +92,7 @@ class StatsWorker : public Napi::AsyncWorker {
9292
baton->dominantRed = dx * 16 + 8;
9393
baton->dominantGreen = dy * 16 + 8;
9494
baton->dominantBlue = dz * 16 + 8;
95-
} catch (vips::VError const &err) {
95+
} catch (std::runtime_error const &err) {
9696
(baton->err).append(err.what());
9797
}
9898
}
@@ -112,7 +112,6 @@ class StatsWorker : public Napi::AsyncWorker {
112112
debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) });
113113
warning = sharp::VipsWarningPop();
114114
}
115-
116115
if (baton->err.empty()) {
117116
// Stats Object
118117
Napi::Object info = Napi::Object::New(env);

src/utilities.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) {
244244
}
245245
// Calculate colour distance
246246
maxColourDistance = image1.dE00(image2).max();
247-
} catch (vips::VError const &err) {
247+
} catch (std::runtime_error const &err) {
248248
throw Napi::Error::New(env, err.what());
249249
}
250250

0 commit comments

Comments
 (0)