diff --git a/examples/acorn/app/models/squirrel.hpp b/examples/acorn/app/models/squirrel.hpp index ed4e0ad1b8..451d8eb260 100644 --- a/examples/acorn/app/models/squirrel.hpp +++ b/examples/acorn/app/models/squirrel.hpp @@ -152,10 +152,21 @@ inline void Squirrel::serialize(rapidjson::Writer& writ writer.EndObject(); } -inline bool Squirrel::deserialize(const rapidjson::Document& doc) { +inline bool Squirrel::deserialize(const rapidjson::Document& doc) +{ + if(not (doc.HasMember("name") and doc["name"].IsString())) + return false; + + if(not (doc.HasMember("age") and doc["age"].IsInt())) + return false; + + if(not (doc.HasMember("occupation") and doc["occupation"].IsString())) + return false; + name_ = doc["name"].GetString(); age_ = doc["age"].GetUint(); occupation_ = doc["occupation"].GetString(); + return true; } diff --git a/examples/acorn/app/routes/squirrels.hpp b/examples/acorn/app/routes/squirrels.hpp index 83418ff0f0..5812027c24 100644 --- a/examples/acorn/app/routes/squirrels.hpp +++ b/examples/acorn/app/routes/squirrels.hpp @@ -60,7 +60,13 @@ class Squirrels : public mana::Router { // create an empty model acorn::Squirrel s; // deserialize it - s.deserialize(doc); + bool ok = s.deserialize(doc); + if(UNLIKELY(not ok)) + { + printf("[Squirrels@POST:/] Could not deserialize squirrel\n"); + res->error({"Parsing Error", "Could not parse data."}); + return; + } // add to bucket auto id = squirrels->capture(s); assert(id == s.key); @@ -74,10 +80,6 @@ class Squirrels : public mana::Router { // send the created entity as response res->send_json(s.json()); } - catch(const Assert_error& e) { - printf("[Squirrels@POST:/] Assert_error: %s\n", e.what()); - res->error({"Parsing Error", "Could not parse data."}); - } catch(const bucket::ConstraintException& e) { printf("[Squirrels@POST:/] ConstraintException: %s\n", e.what()); res->error({"Constraint Exception", e.what()}); diff --git a/lib/mana/include/mana/attributes/json.hpp b/lib/mana/include/mana/attributes/json.hpp index bb834aeb09..fd28ccaaa6 100644 --- a/lib/mana/include/mana/attributes/json.hpp +++ b/lib/mana/include/mana/attributes/json.hpp @@ -26,20 +26,6 @@ #define RAPIDJSON_THROWPARSEEXCEPTION 1 #endif -#include -/** - * - */ -namespace mana { - - struct Assert_error : public std::logic_error { - using std::logic_error::logic_error; - }; //< struct Assert_error - -} // < namespace mana - -#define RAPIDJSON_ASSERT(x) if (!(x)) throw mana::Assert_error(RAPIDJSON_STRINGIFY(x)) - #include #include #include diff --git a/lib/mana/src/middleware/parsley.cpp b/lib/mana/src/middleware/parsley.cpp index 6e3cc13a6d..c8cddf6741 100644 --- a/lib/mana/src/middleware/parsley.cpp +++ b/lib/mana/src/middleware/parsley.cpp @@ -31,19 +31,15 @@ void Parsley::process(mana::Request_ptr req, mana::Response_ptr, mana::Next next auto json = std::make_shared(); // Access the document and parse the body - try { - json->doc().Parse(req->source().body().data()); - #ifdef VERBOSE_WEBSERVER - printf(" Parsed JSON data.\n"); - #endif + bool err = json->doc().Parse(req->source().body().data()).HasParseError(); + #ifdef VERBOSE_WEBSERVER + printf(" Parsed JSON data.\n"); + #endif - // Add the json attribute to the request + if(not err) req->set_attribute(std::move(json)); - } - catch(const Assert_error& e) { - printf(" Parsing error.\n"); - } - + else + printf(" Parsing error\n"); } return (*next)(); diff --git a/mod/rapidjson b/mod/rapidjson index b596f4e990..73063f5002 160000 --- a/mod/rapidjson +++ b/mod/rapidjson @@ -1 +1 @@ -Subproject commit b596f4e99013dcc2d15f3c71124f15e4330beb3a +Subproject commit 73063f5002612c6bf64fe24f851cd5cc0d83eef9