Skip to content

Commit 6002d9b

Browse files
authored
Merge pull request #1197 from psomers3/extend-error-code
Add std::to_string functionality for ErrorCode to ease human meaningful logging
2 parents 4bfceb1 + a35d647 commit 6002d9b

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

cpr/proxyauth.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "cpr/proxyauth.h"
2+
#include "cpr/secure_string.h"
23
#include <string>
34
#include <string_view>
4-
#include "cpr/secure_string.h"
55

66
namespace cpr {
77

include/cpr/async_wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "cpr/response.h"
99

1010
namespace cpr {
11-
enum class [[nodiscard]] CancellationResult{failure, success, invalid_operation};
11+
enum class [[nodiscard]] CancellationResult { failure, success, invalid_operation };
1212

1313
/**
1414
* A class template intended to wrap results of async operations (instances of std::future<T>)

include/cpr/error.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CPR_ERROR_H
22
#define CPR_ERROR_H
33

4+
#include <unordered_map>
45
#include <cstdint>
56
#include <string>
67

@@ -89,6 +90,71 @@ enum class ErrorCode {
8990
UNKNOWN_ERROR = 1000,
9091
};
9192

93+
inline const std::unordered_map<ErrorCode, std::string> error_code_to_string_mapping = {{ErrorCode::OK, "OK"},
94+
{ErrorCode::UNSUPPORTED_PROTOCOL, "UNSUPPORTED_PROTOCOL"},
95+
{ErrorCode::FAILED_INIT, "FAILED_INIT"},
96+
{ErrorCode::URL_MALFORMAT, "URL_MALFORMAT"},
97+
{ErrorCode::NOT_BUILT_IN, "NOT_BUILT_IN"},
98+
{ErrorCode::COULDNT_RESOLVE_PROXY, "COULDNT_RESOLVE_PROXY"},
99+
{ErrorCode::COULDNT_RESOLVE_HOST, "COULDNT_RESOLVE_HOST"},
100+
{ErrorCode::COULDNT_CONNECT, "COULDNT_CONNECT"},
101+
{ErrorCode::WEIRD_SERVER_REPLY, "WEIRD_SERVER_REPLY"},
102+
{ErrorCode::REMOTE_ACCESS_DENIED, "REMOTE_ACCESS_DENIED"},
103+
{ErrorCode::HTTP2, "HTTP2"},
104+
{ErrorCode::PARTIAL_FILE, "PARTIAL_FILE"},
105+
{ErrorCode::QUOTE_ERROR, "QUOTE_ERROR"},
106+
{ErrorCode::HTTP_RETURNED_ERROR, "HTTP_RETURNED_ERROR"},
107+
{ErrorCode::WRITE_ERROR, "WRITE_ERROR"},
108+
{ErrorCode::UPLOAD_FAILED, "UPLOAD_FAILED"},
109+
{ErrorCode::READ_ERROR, "READ_ERROR"},
110+
{ErrorCode::OUT_OF_MEMORY, "OUT_OF_MEMORY"},
111+
{ErrorCode::OPERATION_TIMEDOUT, "OPERATION_TIMEDOUT"},
112+
{ErrorCode::RANGE_ERROR, "RANGE_ERROR"},
113+
{ErrorCode::HTTP_POST_ERROR, "HTTP_POST_ERROR"},
114+
{ErrorCode::SSL_CONNECT_ERROR, "SSL_CONNECT_ERROR"},
115+
{ErrorCode::BAD_DOWNLOAD_RESUME, "BAD_DOWNLOAD_RESUME"},
116+
{ErrorCode::FILE_COULDNT_READ_FILE, "FILE_COULDNT_READ_FILE"},
117+
{ErrorCode::FUNCTION_NOT_FOUND, "FUNCTION_NOT_FOUND"},
118+
{ErrorCode::ABORTED_BY_CALLBACK, "ABORTED_BY_CALLBACK"},
119+
{ErrorCode::BAD_FUNCTION_ARGUMENT, "BAD_FUNCTION_ARGUMENT"},
120+
{ErrorCode::INTERFACE_FAILED, "INTERFACE_FAILED"},
121+
{ErrorCode::TOO_MANY_REDIRECTS, "TOO_MANY_REDIRECTS"},
122+
{ErrorCode::UNKNOWN_OPTION, "UNKNOWN_OPTION"},
123+
{ErrorCode::SETOPT_OPTION_SYNTAX, "SETOPT_OPTION_SYNTAX"},
124+
{ErrorCode::GOT_NOTHING, "GOT_NOTHING"},
125+
{ErrorCode::SSL_ENGINE_NOTFOUND, "SSL_ENGINE_NOTFOUND"},
126+
{ErrorCode::SSL_ENGINE_SETFAILED, "SSL_ENGINE_SETFAILED"},
127+
{ErrorCode::SEND_ERROR, "SEND_ERROR"},
128+
{ErrorCode::RECV_ERROR, "RECV_ERROR"},
129+
{ErrorCode::SSL_CERTPROBLEM, "SSL_CERTPROBLEM"},
130+
{ErrorCode::SSL_CIPHER, "SSL_CIPHER"},
131+
{ErrorCode::PEER_FAILED_VERIFICATION, "PEER_FAILED_VERIFICATION"},
132+
{ErrorCode::BAD_CONTENT_ENCODING, "BAD_CONTENT_ENCODING"},
133+
{ErrorCode::FILESIZE_EXCEEDED, "FILESIZE_EXCEEDED"},
134+
{ErrorCode::USE_SSL_FAILED, "USE_SSL_FAILED"},
135+
{ErrorCode::SEND_FAIL_REWIND, "SEND_FAIL_REWIND"},
136+
{ErrorCode::SSL_ENGINE_INITFAILED, "SSL_ENGINE_INITFAILED"},
137+
{ErrorCode::LOGIN_DENIED, "LOGIN_DENIED"},
138+
{ErrorCode::SSL_CACERT_BADFILE, "SSL_CACERT_BADFILE"},
139+
{ErrorCode::SSL_SHUTDOWN_FAILED, "SSL_SHUTDOWN_FAILED"},
140+
{ErrorCode::AGAIN, "AGAIN"},
141+
{ErrorCode::SSL_CRL_BADFILE, "SSL_CRL_BADFILE"},
142+
{ErrorCode::SSL_ISSUER_ERROR, "SSL_ISSUER_ERROR"},
143+
{ErrorCode::CHUNK_FAILED, "CHUNK_FAILED"},
144+
{ErrorCode::NO_CONNECTION_AVAILABLE, "NO_CONNECTION_AVAILABLE"},
145+
{ErrorCode::SSL_PINNEDPUBKEYNOTMATCH, "SSL_PINNEDPUBKEYNOTMATCH"},
146+
{ErrorCode::SSL_INVALIDCERTSTATUS, "SSL_INVALIDCERTSTATUS"},
147+
{ErrorCode::HTTP2_STREAM, "HTTP2_STREAM"},
148+
{ErrorCode::RECURSIVE_API_CALL, "RECURSIVE_API_CALL"},
149+
{ErrorCode::AUTH_ERROR, "AUTH_ERROR"},
150+
{ErrorCode::HTTP3, "HTTP3"},
151+
{ErrorCode::QUIC_CONNECT_ERROR, "QUIC_CONNECT_ERROR"},
152+
{ErrorCode::PROXY, "PROXY"},
153+
{ErrorCode::SSL_CLIENTCERT, "SSL_CLIENTCERT"},
154+
{ErrorCode::UNRECOVERABLE_POLL, "UNRECOVERABLE_POLL"},
155+
{ErrorCode::TOO_LARGE, "TOO_LARGE"},
156+
{ErrorCode::UNKNOWN_ERROR, "UNKNOWN_ERROR"}};
157+
92158
class Error {
93159
public:
94160
ErrorCode code = ErrorCode::OK;
@@ -108,4 +174,11 @@ class Error {
108174

109175
} // namespace cpr
110176

177+
namespace std {
178+
inline std::string to_string(const cpr::ErrorCode& code) {
179+
return cpr::error_code_to_string_mapping.at(code);
180+
}
181+
} // namespace std
182+
183+
111184
#endif

include/cpr/session.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ class Session : public std::enable_shared_from_this<Session> {
114114
void SetLimitRate(const LimitRate& limit_rate);
115115

116116
/**
117-
* Returns a reference to the content sent in previous request.
118-
**/
117+
* Returns a reference to the content sent in previous request.
118+
**/
119119
[[nodiscard]] const Content& GetContent() const;
120120

121121
/**
122-
* Removes the content sent in previous request from internal state, so it will not be sent with the next request.
123-
* Call this before doing a request that is specified not to send a body, e.g. GET.
124-
**/
122+
* Removes the content sent in previous request from internal state, so it will not be sent with the next request.
123+
* Call this before doing a request that is specified not to send a body, e.g. GET.
124+
**/
125125
void RemoveContent();
126126

127127
// For cancellable requests

test/error_tests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ TEST(ErrorTests, BoolTrueTest) {
9191
EXPECT_TRUE(error);
9292
}
9393

94+
TEST(ErrorTests, StringReprTest) {
95+
Error error;
96+
error.code = ErrorCode::UNSUPPORTED_PROTOCOL;
97+
EXPECT_EQ(std::to_string(error.code), "UNSUPPORTED_PROTOCOL");
98+
}
99+
100+
TEST(ErrorTests, StringReprUnknownTest) {
101+
Error error;
102+
error.code = ErrorCode::UNKNOWN_ERROR;
103+
EXPECT_EQ(std::to_string(error.code), "UNKNOWN_ERROR");
104+
}
105+
106+
94107
int main(int argc, char** argv) {
95108
::testing::InitGoogleTest(&argc, argv);
96109
::testing::AddGlobalTestEnvironment(server);

0 commit comments

Comments
 (0)