include/boost/burl/error.hpp

100.0% Lines (4/4) 100.0% List of functions (2/2) -% Branches (0/0)
error.hpp
f(x) Functions (2)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Mohammad Nejati
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/burl
8 //
9
10 #ifndef BOOST_BURL_ERROR_HPP
11 #define BOOST_BURL_ERROR_HPP
12
13 #include <boost/burl/detail/config.hpp>
14
15 #include <string>
16 #include <system_error>
17
18 namespace boost
19 {
20 namespace burl
21 {
22
23 /** Error codes returned from client operations.
24 */
25 enum class error
26 {
27 /** The URL uses an unsupported scheme.
28
29 The client only supports `http` and `https`
30 target URLs.
31 */
32 unsupported_url_scheme = 1,
33
34 /** The redirect limit was reached.
35
36 The number of followed redirects exceeded
37 @ref client::config::maxredirs.
38 */
39 too_many_redirects,
40
41 /** A redirect response could not be followed.
42
43 The response had a redirect status code but
44 did not contain a usable `Location` header.
45 */
46 bad_redirect_response,
47
48 /** A file changed while it was being sent.
49 */
50 file_changed,
51
52 /** The proxy URL contains an unsupported scheme.
53 */
54 unsupported_proxy_scheme,
55
56 /** The proxy could not connect to the target.
57 */
58 proxy_connect_failed,
59
60 /** Authentication with the proxy failed.
61 */
62 proxy_auth_failed,
63
64 /** The proxy replied with an unsupported protocol version.
65 */
66 proxy_unsupported_version,
67
68 /** The request body size did not match its content length.
69
70 The number of bytes produced by the request body
71 differed from the `Content-Length` declared for the
72 request.
73 */
74 body_size_mismatch,
75 };
76
77 /** Error conditions corresponding to sets of error codes.
78
79 A response with a status code of 400 or above
80 yields an error code whose value is the status
81 code and whose category is @ref burl_category.
82 These codes map to these conditions.
83
84 @par Example
85 @code
86 auto [ec, r] = co_await c.get(url).send();
87 if(ec == burl::condition::client_error)
88 std::cerr << ec.message() << '\n'; // e.g. HTTP 404 Not Found
89 @endcode
90 */
91 enum class condition
92 {
93 /** The response had a 4xx status code.
94 */
95 client_error,
96
97 /** The response had a 5xx status code.
98 */
99 server_error,
100 };
101
102 //----------------------------------------------------------
103
104 /** The error category for burl error codes.
105
106 Values in the range [400, 600) represent HTTP
107 response status codes treated as errors; their
108 messages have the form `"HTTP 404 Not Found"`.
109 4xx and 5xx values compare equal to
110 @ref condition::client_error and
111 @ref condition::server_error respectively.
112
113 @see @ref burl_category.
114 */
115 class BOOST_BURL_DECL error_category : public std::error_category
116 {
117 public:
118 /** Return the name of the category.
119 */
120 char const*
121 name() const noexcept override;
122
123 /** Return a message describing the error code.
124
125 @param ev The error code value.
126 */
127 std::string
128 message(int ev) const override;
129
130 /** Return the default error condition for an error code.
131
132 @param ev The error code value.
133 */
134 std::error_condition
135 default_error_condition(int ev) const noexcept override;
136 };
137
138 /** The error category for burl error conditions.
139
140 @see @ref burl_condition_category.
141 */
142 class BOOST_BURL_DECL condition_category : public std::error_category
143 {
144 public:
145 /** Return the name of the category.
146 */
147 char const*
148 name() const noexcept override;
149
150 /** Return a message describing the error condition.
151
152 @param ev The error condition value.
153 */
154 std::string
155 message(int ev) const override;
156 };
157
158 //----------------------------------------------------------
159
160 /** Return the category for burl error codes.
161
162 @see @ref error, @ref error_category.
163 */
164 BOOST_BURL_DECL
165 std::error_category const&
166 burl_category() noexcept;
167
168 /** Return the category for burl error conditions.
169
170 @see @ref condition, @ref condition_category.
171 */
172 BOOST_BURL_DECL
173 std::error_category const&
174 burl_condition_category() noexcept;
175
176 /** Return an error code for a burl error.
177
178 This function enables implicit conversion of
179 @ref error values to `std::error_code`.
180
181 @param e The error to convert.
182 */
183 inline std::error_code
184 46x make_error_code(error e) noexcept
185 {
186 46x return std::error_code(static_cast<int>(e), burl_category());
187 }
188
189 /** Return an error condition for a burl condition.
190
191 This function enables implicit conversion of
192 @ref condition values to `std::error_condition`.
193
194 @param c The condition to convert.
195 */
196 inline std::error_condition
197 20x make_error_condition(condition c) noexcept
198 {
199 20x return std::error_condition(static_cast<int>(c), burl_condition_category());
200 }
201
202 } // namespace burl
203 } // namespace boost
204
205 //----------------------------------------------------------
206
207 template<>
208 struct std::is_error_code_enum<boost::burl::error> : std::true_type
209 {
210 };
211
212 template<>
213 struct std::is_error_condition_enum<boost::burl::condition> : std::true_type
214 {
215 };
216
217 #endif
218