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