From 87f1e4a7353aede3bd2c238c4ca363a791f7495d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 29 Sep 2025 08:48:42 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80?= =?UTF-8?q?=D1=8B=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuthRegistrationExecutor.h | 14 +++------- src/endpoints_handlers/RootExecutor.h | 27 ++++++++++++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/endpoints_handlers/AuthRegistrationExecutor.h b/src/endpoints_handlers/AuthRegistrationExecutor.h index b61abfa..440093b 100644 --- a/src/endpoints_handlers/AuthRegistrationExecutor.h +++ b/src/endpoints_handlers/AuthRegistrationExecutor.h @@ -8,6 +8,7 @@ #include "IExecutor.h" #include "../DAO/IUserDAO.h" +#include "../exceptions/exception400_bad_request.h" namespace uad { @@ -37,24 +38,17 @@ public: value req_json; value response_body; - response_body.emplace_object(); - try { req_json = json::parse(body); } catch (const system::system_error& err) { - http::response res{http::status::bad_request, req.version()}; - response_body.as_object().emplace("Result", "cannot deserialize json"); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; + throw exception400_bad_request("cannot deserialize json"); } + response_body.emplace_object(); + const std::string login = req_json.as_object().at("login").as_string().c_str(); const std::string password = req_json.as_object().at("password").as_string().c_str(); diff --git a/src/endpoints_handlers/RootExecutor.h b/src/endpoints_handlers/RootExecutor.h index 8fb76fa..480f679 100644 --- a/src/endpoints_handlers/RootExecutor.h +++ b/src/endpoints_handlers/RootExecutor.h @@ -1,3 +1,6 @@ +#include +#include + #include "IExecutor.h" #include "IController.h" #include "Controller.h" @@ -7,6 +10,9 @@ #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" #include "./../helpers/helpers.h" +#include "./../exceptions/exception400_bad_request.h" +#include "./../exceptions/exception409_conflict.h" +#include "./../exceptions/exception422_unprocessable_entity.h" namespace uad { @@ -84,7 +90,26 @@ public: { IRouteExecutor& executor = *maybe_executor_ptr.value(); - return send(executor(std::move(req))); + try + { + boost::beast::http::response res = executor(std::move(req)); + + return send(std::move(res)); + } + catch (const exception400_bad_request& e) + { + boost::beast::http::response res{boost::beast::http::status::bad_request, req.version()}; + boost::json::value response_body; + + response_body.emplace_object(); + response_body.as_object().emplace("Result", e.what()); + + res.body() = serialize(response_body); + res.set(boost::beast::http::field::content_type, "application/json"); + res.content_length(res.body().size()); + + return send(std::move(res)); + } } }