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 /** The response body could not be decoded.
77
78 Decoding the response body according to its
79 `Content-Encoding` failed because the compressed
80 data was corrupt or invalid.
81 */
82 decode_error,
83 };
84
85 /** Error conditions corresponding to sets of error codes.
86
87 A response with a status code of 400 or above
88 yields an error code whose value is the status
89 code and whose category is @ref burl_category.
90 These codes map to these conditions.
91
92 @par Example
93 @code
94 auto [ec, r] = co_await c.get(url).send();
95 if(ec == burl::condition::client_error)
96 std::cerr << ec.message() << '\n'; // e.g. HTTP 404 Not Found
97 @endcode
98 */
99 enum class condition
100 {
101 /** The response had a 4xx status code.
102 */
103 client_error,
104
105 /** The response had a 5xx status code.
106 */
107 server_error,
108 };
109
110 //----------------------------------------------------------
111
112 /** The error category for burl error codes.
113
114 Values in the range [400, 600) represent HTTP
115 response status codes treated as errors; their
116 messages have the form `"HTTP 404 Not Found"`.
117 4xx and 5xx values compare equal to
118 @ref condition::client_error and
119 @ref condition::server_error respectively.
120
121 @see @ref burl_category.
122 */
123 class BOOST_BURL_DECL error_category : public std::error_category
124 {
125 public:
126 /** Return the name of the category.
127 */
128 char const*
129 name() const noexcept override;
130
131 /** Return a message describing the error code.
132
133 @param ev The error code value.
134 */
135 std::string
136 message(int ev) const override;
137
138 /** Return the default error condition for an error code.
139
140 @param ev The error code value.
141 */
142 std::error_condition
143 default_error_condition(int ev) const noexcept override;
144 };
145
146 /** The error category for burl error conditions.
147
148 @see @ref burl_condition_category.
149 */
150 class BOOST_BURL_DECL condition_category : public std::error_category
151 {
152 public:
153 /** Return the name of the category.
154 */
155 char const*
156 name() const noexcept override;
157
158 /** Return a message describing the error condition.
159
160 @param ev The error condition value.
161 */
162 std::string
163 message(int ev) const override;
164 };
165
166 //----------------------------------------------------------
167
168 /** Return the category for burl error codes.
169
170 @see @ref error, @ref error_category.
171 */
172 BOOST_BURL_DECL
173 std::error_category const&
174 burl_category() noexcept;
175
176 /** Return the category for burl error conditions.
177
178 @see @ref condition, @ref condition_category.
179 */
180 BOOST_BURL_DECL
181 std::error_category const&
182 burl_condition_category() noexcept;
183
184 /** Return an error code for a burl error.
185
186 This function enables implicit conversion of
187 @ref error values to `std::error_code`.
188
189 @param e The error to convert.
190 */
191 inline std::error_code
192 71x make_error_code(error e) noexcept
193 {
194 71x return std::error_code(static_cast<int>(e), burl_category());
195 }
196
197 /** Return an error condition for a burl condition.
198
199 This function enables implicit conversion of
200 @ref condition values to `std::error_condition`.
201
202 @param c The condition to convert.
203 */
204 inline std::error_condition
205 20x make_error_condition(condition c) noexcept
206 {
207 20x return std::error_condition(static_cast<int>(c), burl_condition_category());
208 }
209
210 } // namespace burl
211 } // namespace boost
212
213 //----------------------------------------------------------
214
215 template<>
216 struct std::is_error_code_enum<boost::burl::error> : std::true_type
217 {
218 };
219
220 template<>
221 struct std::is_error_condition_enum<boost::burl::condition> : std::true_type
222 {
223 };
224
225 #endif
226