Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::internal::http_error_response Class Reference

Builder for HTTP error responses. More...

#include <http_error.h>

Collaboration diagram for kcenon::network::internal::http_error_response:
Collaboration graph

Static Public Member Functions

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.
 

Detailed Description

Builder for HTTP error responses.

Supports building error responses in JSON and HTML formats. Follows RFC 7807 (Problem Details for HTTP APIs) for JSON format.

Definition at line 180 of file http_error.h.

Member Function Documentation

◆ build_html_error()

auto kcenon::network::internal::http_error_response::build_html_error ( const http_error & error) -> http_response
static

Build HTML format error response.

Parameters
errorError details
Returns
HTTP response with HTML body

Definition at line 221 of file http_error.cpp.

222{
223 http_response response;
224 response.status_code = error.status_code();
225 response.status_message = std::string(get_error_status_text(error.code));
226
227 std::ostringstream html;
228 html << "<!DOCTYPE html>\n";
229 html << "<html lang=\"en\">\n";
230 html << "<head>\n";
231 html << " <meta charset=\"utf-8\">\n";
232 html << " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n";
233 html << " <title>" << response.status_code << " " << escape_html_string(response.status_message)
234 << "</title>\n";
235 html << " <style>\n";
236 html << " body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; "
237 << "margin: 40px auto; max-width: 650px; line-height: 1.6; color: #333; padding: 0 10px; }\n";
238 html << " h1 { color: #c00; border-bottom: 2px solid #eee; padding-bottom: 10px; }\n";
239 html << " .detail { background: #f9f9f9; padding: 15px; border-radius: 5px; }\n";
240 html << " .meta { color: #666; font-size: 0.9em; margin-top: 20px; }\n";
241 html << " </style>\n";
242 html << "</head>\n";
243 html << "<body>\n";
244 html << " <h1>" << response.status_code << " " << escape_html_string(response.status_message)
245 << "</h1>\n";
246
247 if (!error.message.empty())
248 {
249 html << " <p>" << escape_html_string(error.message) << "</p>\n";
250 }
251
252 if (!error.detail.empty())
253 {
254 html << " <div class=\"detail\">\n";
255 html << " <strong>Details:</strong> " << escape_html_string(error.detail) << "\n";
256 html << " </div>\n";
257 }
258
259 html << " <div class=\"meta\">\n";
260 if (!error.request_id.empty())
261 {
262 html << " <p>Request ID: " << escape_html_string(error.request_id) << "</p>\n";
263 }
264 html << " <p><em>NetworkSystem HTTP Server</em></p>\n";
265 html << " </div>\n";
266 html << "</body>\n";
267 html << "</html>\n";
268
269 response.set_body_string(html.str());
270 response.set_header("Content-Type", "text/html; charset=utf-8");
271
272 return response;
273}
auto get_error_status_text(http_error_code code) -> std::string_view
Get status text for HTTP error code.
static auto escape_html_string(const std::string &input) -> std::string
@ error
Black hole detected, reset to base.

References kcenon::network::protocols::quic::error, kcenon::network::internal::escape_html_string(), kcenon::network::internal::get_error_status_text(), kcenon::network::internal::http_response::set_body_string(), kcenon::network::internal::http_response::set_header(), kcenon::network::internal::http_response::status_code, and kcenon::network::internal::http_response::status_message.

Referenced by kcenon::network::core::http_server::build_error_response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_json_error()

auto kcenon::network::internal::http_error_response::build_json_error ( const http_error & error) -> http_response
static

Build JSON format error response (RFC 7807)

Parameters
errorError details
Returns
HTTP response with JSON body

Definition at line 181 of file http_error.cpp.

182{
183 http_response response;
184 response.status_code = error.status_code();
185 response.status_message = std::string(get_error_status_text(error.code));
186
187 // Build RFC 7807 compliant JSON response
188 std::ostringstream json;
189 json << "{\n";
190 json << " \"type\": \"about:blank\",\n";
191 json << " \"title\": \"" << escape_json_string(response.status_message) << "\",\n";
192 json << " \"status\": " << response.status_code << ",\n";
193 json << " \"detail\": \"" << escape_json_string(error.detail.empty() ? error.message : error.detail)
194 << "\"";
195
196 if (!error.request_id.empty())
197 {
198 json << ",\n \"instance\": \"" << escape_json_string(error.request_id) << "\"";
199 }
200
201 // Add timestamp
202 auto time_t_val = std::chrono::system_clock::to_time_t(error.timestamp);
203 std::tm tm_val{};
204#ifdef _WIN32
205 gmtime_s(&tm_val, &time_t_val);
206#else
207 gmtime_r(&time_t_val, &tm_val);
208#endif
209 char time_buf[32];
210 std::strftime(time_buf, sizeof(time_buf), "%Y-%m-%dT%H:%M:%SZ", &tm_val);
211 json << ",\n \"timestamp\": \"" << time_buf << "\"";
212
213 json << "\n}\n";
214
215 response.set_body_string(json.str());
216 response.set_header("Content-Type", "application/problem+json; charset=utf-8");
217
218 return response;
219}
static auto escape_json_string(const std::string &input) -> std::string

References kcenon::network::protocols::quic::error, kcenon::network::internal::escape_json_string(), kcenon::network::internal::get_error_status_text(), kcenon::network::internal::http_response::set_body_string(), kcenon::network::internal::http_response::set_header(), kcenon::network::internal::http_response::status_code, and kcenon::network::internal::http_response::status_message.

Referenced by kcenon::network::core::http_server::build_error_response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_error()

auto kcenon::network::internal::http_error_response::make_error ( http_error_code code,
const std::string & detail = "",
const std::string & request_id = "" ) -> http_error
static

Create http_error from error code.

Parameters
codeHTTP error code
detailOptional detailed message
request_idOptional request ID
Returns
Constructed http_error

Definition at line 275 of file http_error.cpp.

277{
278 http_error error;
279 error.code = code;
280 error.message = std::string(get_error_status_text(code));
281 error.detail = detail;
282 error.request_id = request_id;
283 error.timestamp = std::chrono::system_clock::now();
284 return error;
285}

References kcenon::network::internal::http_error::code, kcenon::network::protocols::quic::error, and kcenon::network::internal::get_error_status_text().

Referenced by kcenon::network::core::http_server::create_error_response().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: