Skip to content

Commit 09f2e5f

Browse files
mana: Remove rapidjson warning by not throwing exception
1 parent 306e732 commit 09f2e5f

4 files changed

Lines changed: 26 additions & 31 deletions

File tree

examples/acorn/app/models/squirrel.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,21 @@ inline void Squirrel::serialize(rapidjson::Writer<rapidjson::StringBuffer>& writ
152152
writer.EndObject();
153153
}
154154

155-
inline bool Squirrel::deserialize(const rapidjson::Document& doc) {
155+
inline bool Squirrel::deserialize(const rapidjson::Document& doc)
156+
{
157+
if(not (doc.HasMember("name") and doc["name"].IsString()))
158+
return false;
159+
160+
if(not (doc.HasMember("age") and doc["age"].IsInt()))
161+
return false;
162+
163+
if(not (doc.HasMember("occupation") and doc["occupation"].IsString()))
164+
return false;
165+
156166
name_ = doc["name"].GetString();
157167
age_ = doc["age"].GetUint();
158168
occupation_ = doc["occupation"].GetString();
169+
159170
return true;
160171
}
161172

examples/acorn/app/routes/squirrels.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ class Squirrels : public mana::Router {
6060
// create an empty model
6161
acorn::Squirrel s;
6262
// deserialize it
63-
s.deserialize(doc);
63+
bool ok = s.deserialize(doc);
64+
if(UNLIKELY(not ok))
65+
{
66+
printf("[Squirrels@POST:/] Could not deserialize squirrel\n");
67+
res->error({"Parsing Error", "Could not parse data."});
68+
return;
69+
}
6470
// add to bucket
6571
auto id = squirrels->capture(s);
6672
assert(id == s.key);
@@ -74,10 +80,6 @@ class Squirrels : public mana::Router {
7480
// send the created entity as response
7581
res->send_json(s.json());
7682
}
77-
catch(const Assert_error& e) {
78-
printf("[Squirrels@POST:/] Assert_error: %s\n", e.what());
79-
res->error({"Parsing Error", "Could not parse data."});
80-
}
8183
catch(const bucket::ConstraintException& e) {
8284
printf("[Squirrels@POST:/] ConstraintException: %s\n", e.what());
8385
res->error({"Constraint Exception", e.what()});

lib/mana/include/mana/attributes/json.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@
2626
#define RAPIDJSON_THROWPARSEEXCEPTION 1
2727
#endif
2828

29-
#include <stdexcept>
30-
/**
31-
*
32-
*/
33-
namespace mana {
34-
35-
struct Assert_error : public std::logic_error {
36-
using std::logic_error::logic_error;
37-
}; //< struct Assert_error
38-
39-
} // < namespace mana
40-
41-
#define RAPIDJSON_ASSERT(x) if (!(x)) throw mana::Assert_error(RAPIDJSON_STRINGIFY(x))
42-
4329
#include <mana/attribute.hpp>
4430
#include <rapidjson/writer.h>
4531
#include <rapidjson/document.h>

lib/mana/src/middleware/parsley.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ void Parsley::process(mana::Request_ptr req, mana::Response_ptr, mana::Next next
3131
auto json = std::make_shared<attribute::Json_doc>();
3232

3333
// Access the document and parse the body
34-
try {
35-
json->doc().Parse(req->source().body().data());
36-
#ifdef VERBOSE_WEBSERVER
37-
printf("<Parsley> Parsed JSON data.\n");
38-
#endif
34+
bool err = json->doc().Parse(req->source().body().data()).HasParseError();
35+
#ifdef VERBOSE_WEBSERVER
36+
printf("<Parsley> Parsed JSON data.\n");
37+
#endif
3938

40-
// Add the json attribute to the request
39+
if(not err)
4140
req->set_attribute(std::move(json));
42-
}
43-
catch(const Assert_error& e) {
44-
printf("<Parsley> Parsing error.\n");
45-
}
46-
41+
else
42+
printf("<Parsley> Parsing error\n");
4743
}
4844

4945
return (*next)();

0 commit comments

Comments
 (0)