magpie
Loading...
Searching...
No Matches
StatusHandlers.hpp
1#pragma once
2
3#include "magpie/data/CommonData.hpp"
4#include "magpie/dsa/RadixTree.hpp"
5#include "magpie/logger/Logger.hpp"
6#include "magpie/transfer/Request.hpp"
7#include "magpie/transfer/Response.hpp"
8#include "magpie/transfer/StatusCode.hpp"
9
10namespace magpie {
11
12template <data::IsCommonData Context>
14 virtual ~StatusHandlerNotFound() = default;
15
20 virtual void onRouteNotFound(
21 Context*, Request&, Response& res,
22 dsa::FindError err
23 ) {
24 if (err == dsa::FindError::IllegalMethod) {
25 res = Response(
26 Status::MethodNotAllowed,
27 "405 method not allowed"
28 );
29 } else if (err == dsa::FindError::NoMatch) {
30 res = Response(
31 Status::NotFound,
32 "404 not found"
33 );
34 }
35 }
36};
37
38template <data::IsCommonData Context>
40 virtual ~StatusHandler500() = default;
41
47 res = Response(
48 magpie::Status::InternalServerError,
49 "500 Internal Server Error"
50 );
51 }
52
58 virtual void tryCall(
59 Context* ctx,
60 Request& req,
61 Response& res,
62 const std::function<void()>& errorHandled
63 ) {
64 try {
65 errorHandled();
66 return;
67 } catch (const std::exception& e) {
68 logger::error("{}", e.what());
69 } catch (...) {
70 logger::error("Caught non-exception type");
71 }
72 provideErrorResponse(ctx, req, res);
73 }
74};
75
76}
Definition Main.cpp:5
Definition Request.hpp:11
Definition Response.hpp:13
Definition StatusHandlers.hpp:39
virtual void tryCall(Context *ctx, Request &req, Response &res, const std::function< void()> &errorHandled)
Definition StatusHandlers.hpp:58
virtual void provideErrorResponse(Context *, Request &, Response &res)
Definition StatusHandlers.hpp:46
Definition StatusHandlers.hpp:13
virtual void onRouteNotFound(Context *, Request &, Response &res, dsa::FindError err)
Definition StatusHandlers.hpp:20