File size: 2,113 Bytes
71e354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**************************************************************************************************
 *
 * Copyright (c) 2019-2026 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
 *
 * This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
 * may not be copied or distributed in any isomorphic form without the prior
 * written consent of Axera Semiconductor (Ningbo) Co., Ltd.
 *
 **************************************************************************************************/
#pragma once

#include <map>
#include <string>

#include "httplib.h"
#include "utils/nlohmann/json.hpp"

enum OPENAI_ERR_CODE {
    OPENAI_ERR_BAD_REQUEST = 400,
    OPENAI_ERR_NOT_FOUND = 404,
    OPENAI_ERR_INTERNAL_SERVER_ERROR = 500
};

static std::map<OPENAI_ERR_CODE, std::string> OPENAI_ERR_TYPE_MAP = {
    {OPENAI_ERR_BAD_REQUEST, "Bad request! Your request was malformed or missing some required parameters."},
    {OPENAI_ERR_NOT_FOUND, "Requested resource does not exist."},
    {OPENAI_ERR_INTERNAL_SERVER_ERROR, "Issue on our side, please check log on server side and contact us."}
};

class ErrorResponse {
private:
    OPENAI_ERR_CODE code_;
    std::string type_;
    std::string message_;
    std::string param_;
    nlohmann::json content_;

public:
    explicit ErrorResponse(OPENAI_ERR_CODE code, const std::string& message, const std::string& param):
        code_(code),
        type_(OPENAI_ERR_TYPE_MAP.at(code)),
        message_(message),
        param_(param) {
        
        content_ = nlohmann::json::parse(R"({
                "error": {
                    "message": "",
                    "type": "",
                    "param": "",
                    "code": 0
                }
            })");
        content_["error"]["message"] = message;
        content_["error"]["type"] = type_;
        content_["error"]["param"] = param_;
        content_["error"]["code"] = code_;
    }

    ErrorResponse& to_res(httplib::Response& res) {
        res.status = code_;
        res.set_content(content_.dump(), "application/json");
        return *this;
    }
};