1 | #ifndef CPR_ERROR_H |
2 | #define CPR_ERROR_H |
3 | |
4 | #include <cstdint> |
5 | #include <string> |
6 | |
7 | #include "cpr/cprtypes.h" |
8 | #include <utility> |
9 | |
10 | namespace cpr { |
11 | |
12 | enum class ErrorCode { |
13 | OK = 0, |
14 | CONNECTION_FAILURE, |
15 | EMPTY_RESPONSE, |
16 | HOST_RESOLUTION_FAILURE, |
17 | INTERNAL_ERROR, |
18 | INVALID_URL_FORMAT, |
19 | NETWORK_RECEIVE_ERROR, |
20 | NETWORK_SEND_FAILURE, |
21 | OPERATION_TIMEDOUT, |
22 | PROXY_RESOLUTION_FAILURE, |
23 | SSL_CONNECT_ERROR, |
24 | SSL_LOCAL_CERTIFICATE_ERROR, |
25 | SSL_REMOTE_CERTIFICATE_ERROR, |
26 | SSL_CACERT_ERROR, |
27 | GENERIC_SSL_ERROR, |
28 | UNSUPPORTED_PROTOCOL, |
29 | REQUEST_CANCELLED, |
30 | TOO_MANY_REDIRECTS, |
31 | UNKNOWN_ERROR = 1000, |
32 | }; |
33 | |
34 | class Error { |
35 | public: |
36 | ErrorCode code = ErrorCode::OK; |
37 | std::string message{}; |
38 | |
39 | Error() = default; |
40 | |
41 | Error(const std::int32_t& curl_code, std::string&& p_error_message) : code{getErrorCodeForCurlError(curl_code)}, message(std::move(p_error_message)) {} |
42 | |
43 | explicit operator bool() const { |
44 | return code != ErrorCode::OK; |
45 | } |
46 | |
47 | private: |
48 | static ErrorCode getErrorCodeForCurlError(std::int32_t curl_code); |
49 | }; |
50 | |
51 | } // namespace cpr |
52 | |
53 | #endif |
54 | |