summaryrefslogtreecommitdiff
path: root/examples/server/utils.hpp
diff options
context:
space:
mode:
authorXuan Son Nguyen <thichthat@gmail.com>2024-03-11 10:56:41 +0100
committerGitHub <noreply@github.com>2024-03-11 10:56:41 +0100
commitcaa106d4e05a0ab94225c220b81f9e2cd522339b (patch)
treed9acbea2801af1260358cfb1e1b964a26ea6fa1f /examples/server/utils.hpp
parent3202361c5b1ba15e695b31209567ef42c22c5c32 (diff)
Server: format error to json (#5961)
* server: format error to json * server: do not crash on grammar error * fix api key test case * revert limit max n_predict * small fix * correct coding style * update completion.js * launch_slot_with_task * update docs * update_slots * update webui * update readme
Diffstat (limited to 'examples/server/utils.hpp')
-rw-r--r--examples/server/utils.hpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/server/utils.hpp b/examples/server/utils.hpp
index f27af81e..48aeef4e 100644
--- a/examples/server/utils.hpp
+++ b/examples/server/utils.hpp
@@ -14,6 +14,17 @@
using json = nlohmann::json;
+// https://community.openai.com/t/openai-chat-list-of-error-codes-and-types/357791/11
+enum error_type {
+ ERROR_TYPE_INVALID_REQUEST,
+ ERROR_TYPE_AUTHENTICATION,
+ ERROR_TYPE_SERVER,
+ ERROR_TYPE_NOT_FOUND,
+ ERROR_TYPE_PERMISSION,
+ ERROR_TYPE_UNAVAILABLE, // custom error
+ ERROR_TYPE_NOT_SUPPORTED, // custom error
+};
+
extern bool server_verbose;
extern bool server_log_json;
@@ -542,3 +553,43 @@ static json format_detokenized_response(const std::string & content) {
{"content", content}
};
}
+
+static json format_error_response(const std::string & message, const enum error_type type) {
+ std::string type_str;
+ int code = 500;
+ switch (type) {
+ case ERROR_TYPE_INVALID_REQUEST:
+ type_str = "invalid_request_error";
+ code = 400;
+ break;
+ case ERROR_TYPE_AUTHENTICATION:
+ type_str = "authentication_error";
+ code = 401;
+ break;
+ case ERROR_TYPE_NOT_FOUND:
+ type_str = "not_found_error";
+ code = 404;
+ break;
+ case ERROR_TYPE_SERVER:
+ type_str = "server_error";
+ code = 500;
+ break;
+ case ERROR_TYPE_PERMISSION:
+ type_str = "permission_error";
+ code = 403;
+ break;
+ case ERROR_TYPE_NOT_SUPPORTED:
+ type_str = "not_supported_error";
+ code = 501;
+ break;
+ case ERROR_TYPE_UNAVAILABLE:
+ type_str = "unavailable_error";
+ code = 503;
+ break;
+ }
+ return json {
+ {"code", code},
+ {"message", message},
+ {"type", type_str},
+ };
+}