Skip to content

Commit 802c945

Browse files
committed
server : use httplib status codes
This commit replaces the status code number with the httplib StatusCode enum values. The motivation for this change is to make the code a little more readable.
1 parent 8ec0583 commit 802c945

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

examples/server/server.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,35 +1047,35 @@ struct server_task_result_rerank : server_task_result {
10471047
// this function maybe used outside of server_task_result_error
10481048
static json format_error_response(const std::string & message, const enum error_type type) {
10491049
std::string type_str;
1050-
int code = 500;
1050+
int code = httplib::StatusCode::InternalServerError_500;
10511051
switch (type) {
10521052
case ERROR_TYPE_INVALID_REQUEST:
10531053
type_str = "invalid_request_error";
1054-
code = 400;
1054+
code = httplib::StatusCode::BadRequest_400;
10551055
break;
10561056
case ERROR_TYPE_AUTHENTICATION:
10571057
type_str = "authentication_error";
1058-
code = 401;
1058+
code = httplib::StatusCode::Unauthorized_401;
10591059
break;
10601060
case ERROR_TYPE_NOT_FOUND:
10611061
type_str = "not_found_error";
1062-
code = 404;
1062+
code = httplib::StatusCode::NotFound_404;
10631063
break;
10641064
case ERROR_TYPE_SERVER:
10651065
type_str = "server_error";
1066-
code = 500;
1066+
code = httplib::StatusCode::InternalServerError_500;
10671067
break;
10681068
case ERROR_TYPE_PERMISSION:
10691069
type_str = "permission_error";
1070-
code = 403;
1070+
code = httplib::StatusCode::Forbidden_403;
10711071
break;
10721072
case ERROR_TYPE_NOT_SUPPORTED:
10731073
type_str = "not_supported_error";
1074-
code = 501;
1074+
code = httplib::StatusCode::NotImplemented_501;
10751075
break;
10761076
case ERROR_TYPE_UNAVAILABLE:
10771077
type_str = "unavailable_error";
1078-
code = 503;
1078+
code = httplib::StatusCode::ServiceUnavailable_503;
10791079
break;
10801080
}
10811081
return json {
@@ -3421,12 +3421,12 @@ int main(int argc, char ** argv) {
34213421
auto res_error = [](httplib::Response & res, const json & error_data) {
34223422
json final_response {{"error", error_data}};
34233423
res.set_content(safe_json_to_str(final_response), MIMETYPE_JSON);
3424-
res.status = json_value(error_data, "code", 500);
3424+
res.status = json_value(error_data, "code", httplib::InternalServerError_500);
34253425
};
34263426

34273427
auto res_ok = [](httplib::Response & res, const json & data) {
34283428
res.set_content(safe_json_to_str(data), MIMETYPE_JSON);
3429-
res.status = 200;
3429+
res.status = httplib::StatusCode::OK_200;
34303430
};
34313431

34323432
svr->set_exception_handler([&res_error](const httplib::Request &, httplib::Response & res, const std::exception_ptr & ep) {
@@ -3445,7 +3445,7 @@ int main(int argc, char ** argv) {
34453445
});
34463446

34473447
svr->set_error_handler([&res_error](const httplib::Request &, httplib::Response & res) {
3448-
if (res.status == 404) {
3448+
if (res.status == httplib::StatusCode::NotFound_404) {
34493449
res_error(res, format_error_response("File Not Found", ERROR_TYPE_NOT_FOUND));
34503450
}
34513451
// for other error codes, we skip processing here because it's already done by res_error()
@@ -3516,7 +3516,7 @@ int main(int argc, char ** argv) {
35163516
auto tmp = string_split<std::string>(req.path, '.');
35173517
if (req.path == "/" || tmp.back() == "html") {
35183518
res.set_content(reinterpret_cast<const char*>(loading_html), loading_html_len, "text/html; charset=utf-8");
3519-
res.status = 503;
3519+
res.status = res.status == httplib::StatusCode::ServiceUnavailable_503;
35203520
} else {
35213521
res_error(res, format_error_response("Loading model", ERROR_TYPE_UNAVAILABLE));
35223522
}
@@ -3692,7 +3692,7 @@ int main(int argc, char ** argv) {
36923692
res.set_header("Process-Start-Time-Unix", std::to_string(res_metrics->t_start));
36933693

36943694
res.set_content(prometheus.str(), "text/plain; version=0.0.4");
3695-
res.status = 200; // HTTP OK
3695+
res.status = httplib::StatusCode::OK_200; // HTTP OK
36963696
};
36973697

36983698
const auto handle_slots_save = [&ctx_server, &res_error, &res_ok, &params](const httplib::Request & req, httplib::Response & res, int id_slot) {
@@ -4321,7 +4321,7 @@ int main(int argc, char ** argv) {
43214321
});
43224322
}
43234323
res_ok(res, result);
4324-
res.status = 200; // HTTP OK
4324+
res.status = httplib::StatusCode::OK_200; // HTTP OK
43254325
};
43264326

43274327
const auto handle_lora_adapters_apply = [&](const httplib::Request & req, httplib::Response & res) {

0 commit comments

Comments
 (0)