Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 2a71521

Browse files
committed
Added minimal error and exception handling
1 parent e2c9a08 commit 2a71521

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

include/Barcode/BarcodeFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919

2020
class BarcodeFactory {
2121
public:
22-
static std::unique_ptr<IBarcodeGenerator> create(const std::string type);
22+
static std::unique_ptr<IBarcodeGenerator> create(const std::string type);
2323
};

src/main.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ZXing/CharacterSet.h>
77
#include <ZXing/MultiFormatWriter.h>
88
#include <memory>
9+
#include <stdexcept>
910
#include <string>
1011
#include <utility>
1112

@@ -18,13 +19,21 @@ int main() {
1819
auto margin = req.url_params.get("margin");
1920
auto size = req.url_params.get("size");
2021
std::unique_ptr<IBarcodeGenerator> generator = BarcodeFactory::create(type);
21-
std::string matrix =
22-
generator->generate(text, std::stoi(margin), std::stoi(size));
2322

23+
std::string matrix;
2424
crow::response resp;
25-
resp.code = 200;
26-
resp.add_header("Content-Type", "image/svg+xml");
27-
resp.body = std::move(matrix);
25+
try {
26+
matrix = generator->generate(text, std::stoi(margin), std::stoi(size));
27+
28+
resp.code = 200;
29+
resp.add_header("Content-Type", "image/svg+xml");
30+
resp.body = std::move(matrix);
31+
} catch (const std::invalid_argument &ex) {
32+
std::cerr << ex.what() << std::endl;
33+
resp.code = 400;
34+
resp.add_header("Content-Type", "text/html");
35+
resp.body = std::move(matrix);
36+
}
2837

2938
return resp;
3039
});

0 commit comments

Comments
 (0)