Skip to content

Commit

Permalink
⚒️add default error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Turmoil committed Dec 1, 2024
1 parent 4963919 commit 54c3a55
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
2 changes: 0 additions & 2 deletions scripts/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ function send_requests() {
curl -s -X POST -H "Content-Type: text/plain" -d "$text" $BASE_URL/echo
fi
echo ""
# rand=$(( RANDOM % 3 )) # sleep 0-3 seconds
# sleep $rand
done
}

Expand Down
2 changes: 2 additions & 0 deletions src/common/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const char* StatusCodeToDescription(int statusCode)
return "Bad Request";
case NOT_FOUND:
return "Not Found";
case METHOD_NOT_ALLOWED:
return "Method Not Allowed";
case INTERNAL_SERVER_ERROR:
return "Internal Server Error";
default:
Expand Down
7 changes: 4 additions & 3 deletions src/core/IRequestDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ void IRequestDispatcher::Dispatch(const Ref<HttpContext>& context)
}
else
{
_logger->Warn("No handler found for path '{}'", path);
_logger->Warn("No handler found for '{} {}'", http::HttpMethodToString(context->Request.Method), path);
}

if (statusCode != http::status::OK)
{
_logger->Error("Error handling request {}: {} {}", path, statusCode, http::StatusCodeToDescription(statusCode));
_logger->Debug("Error occurred when handling request {}: {} {}", path, statusCode,
http::StatusCodeToDescription(statusCode));
handler = _GetErrorHandler(statusCode);
if (handler)
{
Expand All @@ -102,7 +103,7 @@ void IRequestDispatcher::Dispatch(const Ref<HttpContext>& context)
}

DestroyHttpContext(context);
_logger->Debug("Request {} handled", path);
_logger->Debug("Request '{}' handled", path);
}

void IRequestDispatcher::SetLogger(const Ref<Logger>& logger)
Expand Down
5 changes: 5 additions & 0 deletions src/core/WebHostBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "components/MayhemServer.h"
#include "components/RequestDispatcher.h"

#include "impl/DefaultHandlers.h"

#include <cstdlib>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -190,6 +192,9 @@ void WebHostBuilder::_InitializeComponents()

auto dispatcher = _container->Resolve<IRequestDispatcher>();
dispatcher->SetLogger(GetLogger("RequestDispatcher"));

// Register default handlers.
impl::RegisterDefaultHandlers(shared_from_this());
}

void WebHostBuilder::_Preamble() const
Expand Down
46 changes: 46 additions & 0 deletions src/impl/DefaultHandlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "impl/DefaultHandlers.h"

#include "minet/core/WebHostBuilder.h"

MINET_BEGIN

namespace impl
{

static void DefaultErrorHandler(int statusCode, TextResponse& response)
{
response.SetStatusCode(statusCode);
response.Text().assign(http::StatusCodeToDescription(statusCode));
}

void BadRequestHandler(const TextRequest& request, TextResponse& response)
{
DefaultErrorHandler(http::status::BAD_REQUEST, response);
}

void MethodNotAllowedHandler(const TextRequest& request, TextResponse& response)
{
DefaultErrorHandler(http::status::METHOD_NOT_ALLOWED, response);
}

void NotFoundHandler(const TextRequest& request, TextResponse& response)
{
DefaultErrorHandler(http::status::NOT_FOUND, response);
}

void InternalServerErrorHandler(const TextRequest& request, TextResponse& response)
{
DefaultErrorHandler(http::status::INTERNAL_SERVER_ERROR, response);
}

void RegisterDefaultHandlers(const Ref<WebHostBuilder>& builder)
{
builder->Error(http::status::BAD_REQUEST, RequestHandler::Bind(BadRequestHandler))
->Error(http::status::METHOD_NOT_ALLOWED, RequestHandler::Bind(MethodNotAllowedHandler))
->Error(http::status::NOT_FOUND, RequestHandler::Bind(NotFoundHandler))
->Error(http::status::INTERNAL_SERVER_ERROR, RequestHandler::Bind(InternalServerErrorHandler));
}

}; // namespace impl

MINET_END
24 changes: 24 additions & 0 deletions src/impl/DefaultHandlers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "minet/components/RequestHandler.h"

MINET_BEGIN

class WebHostBuilder;

namespace impl
{

void BadRequestHandler(const TextRequest& request, TextResponse& response);

void MethodNotAllowedHandler(const TextRequest& request, TextResponse& response);

void NotFoundHandler(const TextRequest& request, TextResponse& response);

void InternalServerErrorHandler(const TextRequest& request, TextResponse& response);

void RegisterDefaultHandlers(const Ref<WebHostBuilder>& builder);

} // namespace impl

MINET_END

0 comments on commit 54c3a55

Please sign in to comment.