Kurlyk
Loading...
Searching...
No Matches
HttpErrorCategory.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef KURLYK_HEADER_KURLYK_UTILS_HTTP_ERROR_CATEGORY_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_UTILS_HTTP_ERROR_CATEGORY_HPP_INCLUDED
4
7
8namespace kurlyk {
9namespace utils {
10
13 class HttpErrorCategory : public std::error_category {
14 public:
15 const char* name() const noexcept override {
16 return "http";
17 }
18
19 std::string message(int ev) const override {
20 switch (ev) {
21 case 400: return "Bad Request";
22 case 401: return "Unauthorized";
23 case 403: return "Forbidden";
24 case 404: return "Not Found";
25 case 408: return "Request Timeout";
26 case 429: return "Too Many Requests";
27 case 500: return "Internal Server Error";
28 case 502: return "Bad Gateway";
29 case 503: return "Service Unavailable";
30 case 504: return "Gateway Timeout";
31 default: return "HTTP Error " + std::to_string(ev);
32 }
33 }
34 };
35
39 inline std::error_code make_http_error(int status_code) {
40 static HttpErrorCategory category;
41 return std::error_code(status_code, category);
42 }
43
47 inline bool is_http_error(const std::error_code& ec) {
48 return ec.category().name() == std::string("http");
49 }
50
51} // namespace utils
52} // namespace kurlyk
53
54#endif // KURLYK_HEADER_KURLYK_UTILS_HTTP_ERROR_CATEGORY_HPP_INCLUDED
Custom error category that maps HTTP status codes (e.g., 404, 500) to human-readable error messages.
std::string message(int ev) const override
const char * name() const noexcept override
bool is_http_error(const std::error_code &ec)
Checks whether the given error code belongs to the HTTP error category.
std::error_code make_http_error(int status_code)
Creates an std::error_code from an HTTP status code.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...