snake server / handle invalid incoming json messages

This commit is contained in:
Benjamin Sergeant 2020-04-14 15:12:23 -07:00
parent 415f6b4832
commit 783d1d92dd

View File

@ -251,7 +251,22 @@ namespace snake
const AppConfig& appConfig,
const std::string& str)
{
auto pdu = nlohmann::json::parse(str);
nlohmann::json pdu;
try
{
pdu = nlohmann::json::parse(str);
}
catch (const nlohmann::json::parse_error& e)
{
std::stringstream ss;
ss << "malformed json pdu: " << e.what() << " -> " << str << "";
nlohmann::json response = {{"body", {{"error", "invalid_json"},
{"reason", ss.str()}}}};
ws->sendText(response.dump());
return;
}
auto action = pdu["action"];
if (action == "auth/handshake")