Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http_error.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include "http_types.h"
8#include <string>
9#include <string_view>
10#include <chrono>
11
13{
14
19 enum class http_error_code
20 {
21 // Client Errors (4xx)
22 bad_request = 400,
23 unauthorized = 401,
24 payment_required = 402,
25 forbidden = 403,
26 not_found = 404,
28 not_acceptable = 406,
30 request_timeout = 408,
31 conflict = 409,
32 gone = 410,
33 length_required = 411,
36 uri_too_long = 414,
40 im_a_teapot = 418,
43 locked = 423,
45 too_early = 425,
46 upgrade_required = 426,
51
52 // Server Errors (5xx)
54 not_implemented = 501,
55 bad_gateway = 502,
57 gateway_timeout = 504,
61 loop_detected = 508,
62 not_extended = 510,
64 };
65
78 {
80 std::string message;
81 std::string detail;
82 std::string request_id;
83 std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
84
89 auto status_code() const -> int { return static_cast<int>(code); }
90
95 auto is_client_error() const -> bool
96 {
97 int status = status_code();
98 return status >= 400 && status < 500;
99 }
100
105 auto is_server_error() const -> bool
106 {
107 int status = status_code();
108 return status >= 500 && status < 600;
109 }
110 };
111
128
141 {
143 size_t line_number = 0;
144 size_t column_number = 0;
145 std::string context;
146 std::string message;
147
152 auto to_http_error() const -> http_error
153 {
154 http_error err;
156 err.message = "Bad Request";
157 err.detail = message;
158 if (!context.empty())
159 {
160 err.detail += " near: " + context;
161 }
162 return err;
163 }
164 };
165
171 auto get_error_status_text(http_error_code code) -> std::string_view;
172
181 {
182 public:
188 static auto build_json_error(const http_error& error) -> http_response;
189
195 static auto build_html_error(const http_error& error) -> http_response;
196
204 static auto make_error(http_error_code code, const std::string& detail = "",
205 const std::string& request_id = "") -> http_error;
206 };
207
208} // namespace kcenon::network::internal
Builder for HTTP error responses.
Definition http_error.h:181
static auto build_json_error(const http_error &error) -> http_response
Build JSON format error response (RFC 7807)
static auto build_html_error(const http_error &error) -> http_response
Build HTML format error response.
static auto make_error(http_error_code code, const std::string &detail="", const std::string &request_id="") -> http_error
Create http_error from error code.
auto get_error_status_text(http_error_code code) -> std::string_view
Get status text for HTTP error code.
http_error_code
Standard HTTP error codes (RFC 7231)
Definition http_error.h:20
parse_error_type
Types of HTTP parsing errors.
Definition http_error.h:117
@ error
Black hole detected, reset to base.
Structured HTTP error information.
Definition http_error.h:78
auto status_code() const -> int
Get status code as integer.
Definition http_error.h:89
auto is_client_error() const -> bool
Check if this is a client error (4xx)
Definition http_error.h:95
std::chrono::system_clock::time_point timestamp
Definition http_error.h:83
auto is_server_error() const -> bool
Check if this is a server error (5xx)
Definition http_error.h:105
Represents an HTTP response message.
Definition http_types.h:162
Detailed HTTP parsing error information.
Definition http_error.h:141
auto to_http_error() const -> http_error
Convert parse error to http_error.
Definition http_error.h:152