error.hpp
1 #ifndef XXHR_ERROR_H
2 #define XXHR_ERROR_H
3 
4 #include <cstdint>
5 #include <string>
6 #include <sstream>
7 
8 #include <enum.h>
9 
10 #include "xxhrtypes.hpp"
11 #include "defines.hpp"
12 
13 namespace xxhr {
14 
15 
16 //TODO: refactor into system.error_code own category.
17 
18 BETTER_ENUM(ErrorCode, unsigned int,
20  OK = 0,
21  TIMEDOUT,
22  CONNECTION_FAILURE,
23  EMPTY_RESPONSE,
24  HOST_RESOLUTION_FAILURE,
25  INTERNAL_ERROR,
26  INVALID_URL_FORMAT,
27  NETWORK_RECEIVE_ERROR,
28  NETWORK_SEND_FAILURE,
29  PROXY_RESOLUTION_FAILURE,
30  SSL_CONNECT_ERROR,
31  SSL_LOCAL_CERTIFICATE_ERROR,
32  SSL_REMOTE_CERTIFICATE_ERROR,
33  SSL_CACERT_ERROR,
34  GENERIC_SSL_ERROR,
35  UNSUPPORTED_PROTOCOL,
36  UNKNOWN_ERROR = 1000
38 );
39 
46 class Error {
47  public:
48  Error() : code{ErrorCode::OK} {}
49  Error(ErrorCode code) : code{code} {}
50 
51  explicit operator bool() const {
52  return code != +ErrorCode::OK;
53  }
54 
55  ErrorCode code;
56 
58  operator std::string() const {
59  std::stringstream ss;
60  ss << "error:" << static_cast<unsigned int>(this->code) << " " << this->code._to_string();
61  return ss.str();
62  }
63 };
64 
65 inline std::ostream& operator<<(std::ostream& os, const Error& err) {
66  os << std::string(err);
67  return os;
68 }
69 
70 } // namespace xxhr
71 
72 #endif
xxhr::Error
Represents Errors happening at a lower layer than HTTP.
Definition: error.hpp:46
xxhr
main library namespace
Definition: api.hpp:20