Skip to content

Commit

Permalink
fix: some HTTP return codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sileht committed Dec 18, 2020
1 parent fcecdf8 commit 703553f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/jsonapi.cc
Expand Up @@ -195,14 +195,6 @@ namespace dd
return jd;
}

JDoc JsonAPI::dd_conflict_409() const
{
JDoc jd;
jd.SetObject();
render_status(jd, 409, "Conflict");
return jd;
}

JDoc JsonAPI::dd_internal_error_500(const std::string &msg) const
{
JDoc jd;
Expand Down Expand Up @@ -234,7 +226,7 @@ namespace dd
{
JDoc jd;
jd.SetObject();
render_status(jd, 400, "NotFound", 1002, "Service Not Found");
render_status(jd, 404, "NotFound", 1002, "Service Not Found");
return jd;
}

Expand Down Expand Up @@ -338,6 +330,14 @@ namespace dd
return jd;
}

JDoc JsonAPI::dd_service_already_exists_1014() const
{
JDoc jd;
jd.SetObject();
render_status(jd, 409, "Conflict", 1014, "Service already exists");
return jd;
}

std::string JsonAPI::jrender(const JDoc &jst) const
{
rapidjson::StringBuffer buffer;
Expand Down Expand Up @@ -444,6 +444,12 @@ namespace dd
return dd_not_found_404();
}

if (this->service_exists(sname))
{
_logger->error("service `{}` already exists", sname);
return dd_service_already_exists_1014();
}

rapidjson::Document d;
d.Parse<rapidjson::kParseNanAndInfFlag>(jstr.c_str());
if (d.HasParseError())
Expand Down Expand Up @@ -1011,7 +1017,7 @@ namespace dd
if (sname.empty())
return dd_service_not_found_1002();
if (!this->service_exists(sname))
return dd_not_found_404();
return dd_service_not_found_1002();
auto hit = this->get_service_it(sname);
APIData ad = mapbox::util::apply_visitor(visitor_status(), (*hit).second);
JDoc jst = dd_ok_200();
Expand Down
2 changes: 1 addition & 1 deletion src/jsonapi.h
Expand Up @@ -66,7 +66,6 @@ namespace dd
JDoc dd_bad_request_400(const std::string &msg = "") const;
JDoc dd_forbidden_403() const;
JDoc dd_not_found_404() const;
JDoc dd_conflict_409() const;
JDoc dd_internal_error_500(const std::string &msg = "") const;

// specific errors
Expand All @@ -84,6 +83,7 @@ namespace dd
JDoc dd_sim_search_error_1011() const;
JDoc dd_action_bad_request_1012(const std::string &what = "") const;
JDoc dd_action_internal_error_1013(const std::string &what = "") const;
JDoc dd_service_already_exists_1014() const;

// JSON rendering
std::string jrender(const JDoc &jst) const;
Expand Down

0 comments on commit 703553f

Please sign in to comment.