Make WebRoot use the new error handling system.

This commit is contained in:
Relintai 2022-01-08 11:51:39 +01:00
parent fd106fc785
commit 163432e147
2 changed files with 22 additions and 17 deletions

View File

@ -69,13 +69,13 @@ void WebRoot::default_routing_middleware(Object *instance, Request *request) {
request->next_stage(); request->next_stage();
} }
void WebRoot::default_fallback_error_handler(int error_code, Request *request) { void WebRoot::default_fallback_error_handler(Request *request, int error_code) {
request->compiled_body = default_generic_error_body; request->compiled_body = default_generic_error_body;
request->send(); request->send();
} }
void WebRoot::default_404_error_handler(int error_code, Request *request) { void WebRoot::default_404_error_handler(Request *request, int error_code) {
request->compiled_body = default_error_404_body; request->compiled_body = default_error_404_body;
request->send(); request->send();
} }
@ -93,10 +93,22 @@ void WebRoot::handle_request_main(Request *request) {
// normal routing // normal routing
WebRouterNode::handle_request_main(request); WebRouterNode::handle_request_main(request);
} }
/*
void WebRoot::handle_error_send_request(Request *request, const int error_code) {
}*/ void WebRoot::handle_error_send_request(Request *request, const int error_code) {
std::function<void(Request *, int)> func = error_handler_map[error_code];
if (!func) {
if (!default_error_handler_func) {
WebNode::handle_error_send_request(request, error_code);
return;
}
default_error_handler_func(request, error_code);
return;
}
func(request, error_code);
}
bool WebRoot::try_send_wwwroot_file(Request *request) { bool WebRoot::try_send_wwwroot_file(Request *request) {
const String &path = request->get_path_full(); const String &path = request->get_path_full();
@ -111,14 +123,7 @@ bool WebRoot::try_send_wwwroot_file(Request *request) {
} }
void WebRoot::send_error(int error_code, Request *request) { void WebRoot::send_error(int error_code, Request *request) {
std::function<void(int, Request *)> func = error_handler_map[error_code];
if (!func) {
default_error_handler_func(error_code, request);
return;
}
func(error_code, request);
} }
void WebRoot::send_file(const std::string &path, Request *request) { void WebRoot::send_file(const std::string &path, Request *request) {

View File

@ -35,14 +35,14 @@ public:
static std::string default_generic_error_body; static std::string default_generic_error_body;
void handle_request_main(Request *request); void handle_request_main(Request *request);
//void handle_error_send_request(Request *request, const int error_code); void handle_error_send_request(Request *request, const int error_code);
bool try_send_wwwroot_file(Request *request); bool try_send_wwwroot_file(Request *request);
void send_error(int error_code, Request *request); void send_error(int error_code, Request *request);
void send_file(const std::string &path, Request *request); void send_file(const std::string &path, Request *request);
static void default_fallback_error_handler(int error_code, Request *request); static void default_fallback_error_handler(Request *request, int error_code);
static void default_404_error_handler(int error_code, Request *request); static void default_404_error_handler(Request *request, int error_code);
virtual void load_settings(); virtual void load_settings();
virtual void setup_routes(); virtual void setup_routes();
@ -66,8 +66,8 @@ public:
std::map<std::string, HandlerInstance> main_route_map; std::map<std::string, HandlerInstance> main_route_map;
std::vector<HandlerInstance> middlewares; std::vector<HandlerInstance> middlewares;
std::map<int, std::function<void(int, Request *)> > error_handler_map; std::map<int, std::function<void(Request *, int)> > error_handler_map;
std::function<void(int, Request *)> default_error_handler_func; std::function<void(Request *, int)> default_error_handler_func;
protected: protected:
std::mutex _update_registered_requests_mutex; std::mutex _update_registered_requests_mutex;