feat: add custom 400 error page and update configuration for error handling

This commit is contained in:
whaffman 2025-09-30 10:41:53 +02:00
parent 949c788259
commit b7f8947440
4 changed files with 89 additions and 0 deletions

73
400.html Normal file
View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>400 Bad Request</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">400</div>
<div class="message">Oops! Bad Request.<br>Your browser sent a request that this server could not understand.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -1,4 +1,7 @@
autoindex on autoindex on
error_page 400 ./400.html
server { server {
listen 8080; listen 8080;

View File

@ -95,4 +95,13 @@ ServerConfig *ConfigManager::getMatchingServerConfig(const std::string &host, in
} }
Log::warning("No matching server config found for host: " + host + " and port: " + std::to_string(port)); Log::warning("No matching server config found for host: " + host + " and port: " + std::to_string(port));
return nullptr; return nullptr;
}
GlobalConfig *ConfigManager::getGlobalConfig() const
{
if (!initialized_)
{
throw std::runtime_error("ConfigManager is not initialized.");
}
return globalConfig_.get();
} }

View File

@ -1,3 +1,4 @@
#include "webserv/config/ConfigManager.hpp"
#include <webserv/config/AConfig.hpp> #include <webserv/config/AConfig.hpp>
#include <webserv/handler/ErrorHandler.hpp> #include <webserv/handler/ErrorHandler.hpp>
#include <webserv/http/HttpConstants.hpp> // for StatusCode #include <webserv/http/HttpConstants.hpp> // for StatusCode
@ -30,11 +31,14 @@ std::string ErrorHandler::generateErrorPage(int statusCode, AConfig *config)
if (config != nullptr) if (config != nullptr)
{ {
config = ConfigManager::getInstance().getGlobalConfig();
Log::info("Checking for custom error page for status code: " + std::to_string(statusCode));
std::string customPage = config->getErrorPage(statusCode); std::string customPage = config->getErrorPage(statusCode);
if (!customPage.empty()) if (!customPage.empty())
{ {
return getErrorPageFile(customPage); return getErrorPageFile(customPage);
} }
Log::warning("No custom error page foundin config for status code: " + std::to_string(statusCode));
} }
return generateDefaultErrorPage(statusCode); return generateDefaultErrorPage(statusCode);
} }