forked from expresscpp/expresscpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.cpp
More file actions
57 lines (44 loc) · 1.57 KB
/
error_handler.cpp
File metadata and controls
57 lines (44 loc) · 1.57 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
#include "expresscpp/console.hpp"
#include "expresscpp/expresscpp.hpp"
using namespace expresscpp;
int main() {
ExpressCpp app;
auto ErrorMiddleware = [&](auto ec, auto /*req*/, auto res, auto /*next*/) {
std::cerr << "Internal Error: " << ec.message() << std::endl;
res->SetStatus(500);
res->Send("Internal Error");
};
app.Error(ErrorMiddleware);
struct User {
std::string name;
uint8_t age;
};
std::vector<User> users{{"john", 30}, {"steve", 40}};
app.Get("/users/:userId", [&](auto req, auto res, auto /*next*/) {
auto params = req->GetParams();
if ((params.find("userId") != params.end())) {
auto id = params.at("userId");
const int userId = std::stoi(id);
// ops, possible the user does not exist -> exception
auto user = users.at(userId);
res->Json(fmt::format("{{\"name\":\"{}\", \"age\":{}}}", user.name, user.age));
} else {
res->SetStatus(404);
res->Send(R"({"error":"user not found"})");
}
});
constexpr uint16_t port = 8081;
app.Listen(port,
[=](auto ec) {
if (ec) {
std::cerr << "ERROR: " << ec.message() << std::endl;
std::cerr << "exiting..." << std::endl;
exit(1);
}
std::cout << fmt::format(R"(you can try now (should work): "curl http://localhost:{}/users/0")", port)
<< std::endl;
std::cout << fmt::format(R"(and (should fail): "curl http://localhost:{}/users/10")", port) << std::endl;
})
.Run();
return 0;
}