Kurlyk
Loading...
Searching...
No Matches
HttpErrorCategory.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_UTILS_HTTP_ERROR_CATEGORY_HPP_INCLUDED
3#define _KURLYK_UTILS_HTTP_ERROR_CATEGORY_HPP_INCLUDED
4
7
8namespace kurlyk::utils {
9
12 class HttpErrorCategory : public std::error_category {
13 public:
14 const char* name() const noexcept override {
15 return "http";
16 }
17
18 std::string message(int ev) const override {
19 switch (ev) {
20 case 400: return "Bad Request";
21 case 401: return "Unauthorized";
22 case 403: return "Forbidden";
23 case 404: return "Not Found";
24 case 408: return "Request Timeout";
25 case 429: return "Too Many Requests";
26 case 500: return "Internal Server Error";
27 case 502: return "Bad Gateway";
28 case 503: return "Service Unavailable";
29 case 504: return "Gateway Timeout";
30 default: return "HTTP Error " + std::to_string(ev);
31 }
32 }
33 };
34
38 inline std::error_code make_http_error(int status_code) {
39 static HttpErrorCategory category;
40 return std::error_code(status_code, category);
41 }
42
46 inline bool is_http_error(const std::error_code& ec) {
47 return ec.category().name() == std::string("http");
48 }
49
50} // namespace kurlyk::utils
51
52#endif // _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.