diff --git a/include/CustomAssert.h b/include/CustomAssert.h index a294e46..3251a73 100644 --- a/include/CustomAssert.h +++ b/include/CustomAssert.h @@ -61,12 +61,14 @@ inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const ch // In release build, these macros turn into NOPs. The compiler will optimize these out. #define Assert(cond) \ do { \ + bool result = (cond); \ if (!result) { \ Sentry.LogAssert(#cond, _file_basename, _line, __func__); \ } \ } while (false) #define AssertNotReachable() \ do { \ + bool result = (cond); \ if (!result) { \ Sentry.LogAssert("code is unreachable", _file_basename, _line, __func__); \ } \ diff --git a/include/Http.h b/include/Http.h index e0eb564..2e439fc 100644 --- a/include/Http.h +++ b/include/Http.h @@ -5,5 +5,5 @@ namespace Http { std::string GET(const std::string& host, int port, const std::string& target); -std::string POST(const std::string& host, const std::string& target, const std::unordered_map& fields, const std::string& body, bool json); -} \ No newline at end of file +std::string POST(const std::string& host, const std::string& target, const std::unordered_map& fields, const std::string& body, bool json, int* status = nullptr); +} diff --git a/src/Http.cpp b/src/Http.cpp index 9674a81..a9090de 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -54,7 +54,7 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ } } -std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map& fields, const std::string& body, bool json) { +std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map& fields, const std::string& body, bool json, int* status) { try { net::io_context io; @@ -136,6 +136,9 @@ std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map response_data; response_data["reponse-code"] = std::to_string(response.result_int()); + if (status) { + *status = response.result_int(); + } for (const auto& header : response.base()) { // need to do explicit casts to convert string_view to string // since string_view may not be null-terminated (and in fact isn't, here) diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index dd9ecf9..96ffdac 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -36,7 +36,8 @@ void THeartbeatThread::operator()() { Body += "&pps=" + Application::PPS(); auto Target = "/heartbeat"; - T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false); + int ResponseCode = -1; + T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false, &ResponseCode); if (T.substr(0, 2) != "20") { auto SentryReportError = [&](const std::string& transaction) { @@ -47,7 +48,7 @@ void THeartbeatThread::operator()() { { { "response-body", T }, { "request-body", Body } }); Sentry.SetTransaction(transaction); - Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format"); + Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format (" + std::to_string(ResponseCode) + ")"); } }; SentryReportError(Application::GetBackendHostname() + Target); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 6977c60..0f03d0c 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -283,8 +283,9 @@ void TNetwork::Authentication(SOCKET TCPSock) { auto RequestString = R"({"key":")" + Rc + "\"}"; auto Target = "/pkToUser"; + int ResponseCode = -1; if (!Rc.empty()) { - Rc = Http::POST(Application::GetBackendUrlForAuth(), Target, {}, RequestString, true); + Rc = Http::POST(Application::GetBackendUrlForAuth(), Target, {}, RequestString, true, &ResponseCode); } json::Document AuthResponse; @@ -302,7 +303,7 @@ void TNetwork::Authentication(SOCKET TCPSock) { { { "response-body", Rc }, { "key", RequestString } }); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); - Sentry.Log(SENTRY_LEVEL_ERROR, "default", "auth: wrong backend response format"); + Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format (" + std::to_string(ResponseCode) + ")"); return; } else if (Rc == "0") { auto Lock = Sentry.CreateExclusiveContext(); @@ -310,7 +311,7 @@ void TNetwork::Authentication(SOCKET TCPSock) { { { "response-body", Rc }, { "key", RequestString } }); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); - Sentry.Log(SENTRY_LEVEL_INFO, "default", "backend returned 0 instead of json"); + Sentry.Log(SENTRY_LEVEL_INFO, "default", "backend returned 0 instead of json (" + std::to_string(ResponseCode) + ")"); } if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString()