src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 5
0.0%
Branches:
0 / 4
0.0%
Functions:
0 / 2
0.0%

utils/http_utils.hpp
Line Branch Exec Source
1 #pragma once
2
3 #include <format>
4 #include <stdexcept>
5 #include <string>
6
7 /**
8 * @brief Exception thrown when an HTTP request fails with a non-2xx status code or CURL error.
9 *
10 * Inherits from std::runtime_error and stores the HTTP status code for detailed error handling.
11 * Allows calling code to distinguish between different HTTP errors (e.g., 404 vs 500) and handle
12 * them appropriately.
13 *
14 * @see CurlUtils::fetch_web_content()
15 *
16 * @example
17 * @code
18 * try {
19 * std::string response = curl->fetch_web_content(url);
20 * } catch (const HttpException& e) {
21 * if (e.get_status_code() == 404) {
22 * std::cout << "Resource not found" << std::endl;
23 * } else {
24 * std::cout << "HTTP error: " << e.what() << std::endl;
25 * }
26 * }
27 * @endcode
28 */
29 class HttpException : public std::runtime_error {
30 private:
31 /**
32 * The HTTP status code that triggered the exception.
33 */
34 long status_code;
35
36 public:
37 /**
38 * @brief Constructs an HttpException with HTTP status code and URL information.
39 *
40 * The exception message is formatted as "HTTP {status_code}: {url}" and is accessible
41 * via std::exception::what().
42 *
43 * @param status_code The HTTP status code (e.g., 404, 500). Pass 0 for CURL errors.
44 * @param url The URL that was requested when the error occurred.
45 *
46 * @example
47 * @code
48 * throw HttpException(404, "http://localhost:6333/collections/missing");
49 * @endcode
50 */
51 ✗ HttpException(long status_code, const std::string& url)
52
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ : std::runtime_error(std::format("HTTP {}: {}", status_code, url)), status_code(status_code) {
53 ✗ }
54
55 /**
56 * @brief Retrieves the HTTP status code associated with this exception.
57 *
58 * Useful for determining whether an error is recoverable (e.g., 404 means resource doesn't exist)
59 * or critical (e.g., 500 means server error).
60 *
61 * @return The HTTP status code as a long. Returns 0 for CURL connection errors.
62 *
63 * @example
64 * @code
65 * catch (const HttpException& e) {
66 * if (e.get_status_code() == 404) {
67 * // Handle not found
68 * }
69 * }
70 * @endcode
71 */
72 ✗ long get_status_code() const {
73 ✗ return status_code;
74 }
75 };
76