diff --git a/include/Barcode/BarcodeFactory.h b/include/Barcode/BarcodeFactory.h index 75becd9..4d94915 100644 --- a/include/Barcode/BarcodeFactory.h +++ b/include/Barcode/BarcodeFactory.h @@ -19,5 +19,5 @@ class BarcodeFactory { public: - static std::unique_ptr create(const std::string type); + static std::unique_ptr create(const std::string type); }; diff --git a/src/main.cpp b/src/main.cpp index a37b2c1..842321c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -18,16 +19,24 @@ int main() { auto margin = req.url_params.get("margin"); auto size = req.url_params.get("size"); std::unique_ptr generator = BarcodeFactory::create(type); - std::string matrix = - generator->generate(text, std::stoi(margin), std::stoi(size)); + std::string matrix; crow::response resp; - resp.code = 200; - resp.add_header("Content-Type", "image/svg+xml"); - resp.body = std::move(matrix); + try { + matrix = generator->generate(text, std::stoi(margin), std::stoi(size)); + + resp.code = 200; + resp.add_header("Content-Type", "image/svg+xml"); + resp.body = std::move(matrix); + } catch (const std::invalid_argument &ex) { + std::cerr << ex.what() << std::endl; + resp.code = 400; + resp.add_header("Content-Type", "text/html"); + resp.body = std::move(matrix); + } return resp; }); - app.port(80).multithreaded().run(); + app.port(8080).multithreaded().run(); }