-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.h
More file actions
95 lines (90 loc) · 3.5 KB
/
error_handler.h
File metadata and controls
95 lines (90 loc) · 3.5 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef ERROR_HANDLER_H
#define ERROR_HANDLER_H
#include <functional>
#include "error_code.h"
#include "response.h"
#include "crow.h"
template <typename T>
struct crow_error_handler;
template <typename Ret, typename...Args>
struct crow_error_handler<Ret(const crow::request&, crow::response&, Args...)>
{
using FUNC_TYPE = Ret(const crow::request&, crow::response&, Args...);
using TRANS_FUNC_TYPE = std::function<Ret(const crow::request&, crow::response&, Args...)>;
static TRANS_FUNC_TYPE generate(FUNC_TYPE f)
{
return [f](const crow::request& req, crow::response& res, Args...args) {
try {
return f(req,res,args...);
} catch (CError &e) {
res.code = 500;
res.write(responseError(e.getCode(),e.getMsg(),std::string()));
res.end();
} catch (std::exception &e) {
res.code = 500;
res.write(responseError(500, "Internal Error", std::string(e.what())));
res.end();
}
};
}
};
template <typename Ret, typename...Args>
struct crow_error_handler<std::function<Ret(const crow::request&, crow::response&, Args...)>>
{
using FUNC_TYPE = std::function<Ret(const crow::request&, crow::response&, Args...)>;
using TRANS_FUNC_TYPE = std::function<Ret(const crow::request&, crow::response&, Args...)>;
static TRANS_FUNC_TYPE generate(FUNC_TYPE f)
{
return [f](const crow::request& req, crow::response& res, Args...args) {
try {
return f(req,res,args...);
} catch (CError &e) {
res.code = 500;
res.write(responseError(e.getCode(),e.getMsg(),std::string()));
res.end();
} catch (std::exception &e) {
res.code = 500;
res.write(responseError(500, "Internal Error", std::string(e.what())));
res.end();
}
};
}
};
template <typename Ret, typename...Args>
struct crow_error_handler<Ret(const crow::request&, Args...)>
{
using FUNC_TYPE = Ret(const crow::request&, Args...);
using TRANS_FUNC_TYPE = std::function<Ret(const crow::request&, Args...)>;
static TRANS_FUNC_TYPE generate(FUNC_TYPE f)
{
return [f](const crow::request& req, Args...args) {
try {
return f(req,args...);
} catch (CError &e) {
return crow::response(500, responseError(e.getCode(),e.getMsg(),std::string()));
} catch (std::exception &e) {
return crow::response(500, responseError(500, "Internal Error", std::string(e.what())));
}
};
}
};
template <typename Ret, typename...Args>
struct crow_error_handler<std::function<Ret(const crow::request&, Args...)>>
{
using FUNC_TYPE = std::function<Ret(const crow::request&, Args...)>;
using TRANS_FUNC_TYPE = std::function<Ret(const crow::request&, Args...)>;
static TRANS_FUNC_TYPE generate(FUNC_TYPE f)
{
return [f](const crow::request& req, Args...args) {
try {
return f(req,args...);
} catch (CError &e) {
return crow::response(500, responseError(e.getCode(),e.getMsg(),std::string()));
} catch (std::exception &e) {
return crow::response(500, responseError(500, "Internal Error", std::string(e.what())));
}
};
}
};
#define CROW_ERROR_HANDLER(func) crow_error_handler<decltype(func)>::generate(func)
#endif // ERROR_HANDLER_H