Skip to content

Commit 9d9908c

Browse files
authored
Enhance: Use unordered_map for CURL error mapping (#1142)
* Removed obsolete error code * Added all non-version related error codes to map * Added all version related error codes to map * Refactored getErrorCodeForCurlError to use new unordered_map * Added PARTIAL_FILE error to unordered_map * Cleanup of includes and blank lines * Reformat of code using clang-format * Suppressed Clang-Tidy warning with NOLINT * Readd includes for clang-tidy
1 parent 102b2e6 commit 9d9908c

File tree

2 files changed

+101
-137
lines changed

2 files changed

+101
-137
lines changed

cpr/error.cpp

Lines changed: 101 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,202 +1,167 @@
11
#include "cpr/error.h"
2+
#include <curl/curlver.h>
3+
#include <unordered_map>
24
#include <cstdint>
35
#include <curl/curl.h>
4-
#include <curl/curlver.h>
56

67
namespace cpr {
7-
ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
8-
switch (curl_code) {
9-
case CURLE_OK:
10-
return ErrorCode::OK;
11-
case CURLE_UNSUPPORTED_PROTOCOL:
12-
return ErrorCode::UNSUPPORTED_PROTOCOL;
13-
case CURLE_FAILED_INIT:
14-
return ErrorCode::FAILED_INIT;
15-
case CURLE_URL_MALFORMAT:
16-
return ErrorCode::URL_MALFORMAT;
17-
case CURLE_NOT_BUILT_IN:
18-
return ErrorCode::NOT_BUILT_IN;
19-
case CURLE_COULDNT_RESOLVE_PROXY:
20-
return ErrorCode::COULDNT_RESOLVE_PROXY;
21-
case CURLE_COULDNT_RESOLVE_HOST:
22-
return ErrorCode::COULDNT_RESOLVE_HOST;
23-
case CURLE_COULDNT_CONNECT:
24-
return ErrorCode::COULDNT_CONNECT;
8+
static const std::unordered_map<std::int32_t, ErrorCode> curl_error_map = { // NOLINT - (needed because of static init)
9+
{CURLE_OK, ErrorCode::OK},
10+
{CURLE_UNSUPPORTED_PROTOCOL, ErrorCode::UNSUPPORTED_PROTOCOL},
11+
{CURLE_FAILED_INIT, ErrorCode::FAILED_INIT},
12+
{CURLE_URL_MALFORMAT, ErrorCode::URL_MALFORMAT},
13+
{CURLE_NOT_BUILT_IN, ErrorCode::NOT_BUILT_IN},
14+
{CURLE_COULDNT_RESOLVE_PROXY, ErrorCode::COULDNT_RESOLVE_PROXY},
15+
{CURLE_COULDNT_RESOLVE_HOST, ErrorCode::COULDNT_RESOLVE_HOST},
16+
{CURLE_COULDNT_CONNECT, ErrorCode::COULDNT_CONNECT},
17+
2518
// Name changed in curl >= 7.51.0.
2619
#if LIBCURL_VERSION_NUM >= 0x073300
27-
case CURLE_WEIRD_SERVER_REPLY:
20+
{CURLE_WEIRD_SERVER_REPLY, ErrorCode::WEIRD_SERVER_REPLY},
2821
#else
29-
case CURLE_FTP_WEIRD_SERVER_REPLY:
30-
#endif
31-
return ErrorCode::WEIRD_SERVER_REPLY;
32-
case CURLE_REMOTE_ACCESS_DENIED:
33-
return ErrorCode::REMOTE_ACCESS_DENIED;
34-
case CURLE_HTTP2:
35-
return ErrorCode::HTTP2;
36-
case CURLE_QUOTE_ERROR:
37-
return ErrorCode::QUOTE_ERROR;
38-
case CURLE_HTTP_RETURNED_ERROR:
39-
return ErrorCode::HTTP_RETURNED_ERROR;
40-
case CURLE_WRITE_ERROR:
41-
return ErrorCode::WRITE_ERROR;
42-
case CURLE_UPLOAD_FAILED:
43-
return ErrorCode::UPLOAD_FAILED;
44-
case CURLE_READ_ERROR:
45-
return ErrorCode::READ_ERROR;
46-
case CURLE_OUT_OF_MEMORY:
47-
return ErrorCode::OUT_OF_MEMORY;
48-
case CURLE_OPERATION_TIMEDOUT:
49-
return ErrorCode::OPERATION_TIMEDOUT;
50-
case CURLE_RANGE_ERROR:
51-
return ErrorCode::RANGE_ERROR;
52-
case CURLE_HTTP_POST_ERROR:
53-
return ErrorCode::HTTP_POST_ERROR;
54-
case CURLE_SSL_CONNECT_ERROR:
55-
return ErrorCode::SSL_CONNECT_ERROR;
56-
case CURLE_BAD_DOWNLOAD_RESUME:
57-
return ErrorCode::BAD_DOWNLOAD_RESUME;
58-
case CURLE_FILE_COULDNT_READ_FILE:
59-
return ErrorCode::FILE_COULDNT_READ_FILE;
60-
case CURLE_FUNCTION_NOT_FOUND:
61-
return ErrorCode::FUNCTION_NOT_FOUND;
62-
case CURLE_ABORTED_BY_CALLBACK:
63-
return ErrorCode::ABORTED_BY_CALLBACK;
64-
case CURLE_BAD_FUNCTION_ARGUMENT:
65-
return ErrorCode::BAD_FUNCTION_ARGUMENT;
66-
case CURLE_INTERFACE_FAILED:
67-
return ErrorCode::INTERFACE_FAILED;
68-
case CURLE_OBSOLETE46:
69-
return ErrorCode::OBSOLETE46;
70-
case CURLE_TOO_MANY_REDIRECTS:
71-
return ErrorCode::TOO_MANY_REDIRECTS;
72-
case CURLE_UNKNOWN_OPTION:
73-
return ErrorCode::UNKNOWN_OPTION;
22+
{CURLE_FTP_WEIRD_SERVER_REPLY, ErrorCode::WEIRD_SERVER_REPLY},
23+
#endif
24+
25+
{CURLE_REMOTE_ACCESS_DENIED, ErrorCode::REMOTE_ACCESS_DENIED},
26+
{CURLE_HTTP2, ErrorCode::HTTP2},
27+
{CURLE_QUOTE_ERROR, ErrorCode::QUOTE_ERROR},
28+
{CURLE_HTTP_RETURNED_ERROR, ErrorCode::HTTP_RETURNED_ERROR},
29+
{CURLE_WRITE_ERROR, ErrorCode::WRITE_ERROR},
30+
{CURLE_UPLOAD_FAILED, ErrorCode::UPLOAD_FAILED},
31+
{CURLE_READ_ERROR, ErrorCode::READ_ERROR},
32+
{CURLE_OUT_OF_MEMORY, ErrorCode::OUT_OF_MEMORY},
33+
{CURLE_OPERATION_TIMEDOUT, ErrorCode::OPERATION_TIMEDOUT},
34+
{CURLE_RANGE_ERROR, ErrorCode::RANGE_ERROR},
35+
{CURLE_HTTP_POST_ERROR, ErrorCode::HTTP_POST_ERROR},
36+
{CURLE_SSL_CONNECT_ERROR, ErrorCode::SSL_CONNECT_ERROR},
37+
{CURLE_BAD_DOWNLOAD_RESUME, ErrorCode::BAD_DOWNLOAD_RESUME},
38+
{CURLE_FILE_COULDNT_READ_FILE, ErrorCode::FILE_COULDNT_READ_FILE},
39+
{CURLE_FUNCTION_NOT_FOUND, ErrorCode::FUNCTION_NOT_FOUND},
40+
{CURLE_ABORTED_BY_CALLBACK, ErrorCode::ABORTED_BY_CALLBACK},
41+
{CURLE_BAD_FUNCTION_ARGUMENT, ErrorCode::BAD_FUNCTION_ARGUMENT},
42+
{CURLE_INTERFACE_FAILED, ErrorCode::INTERFACE_FAILED},
43+
{CURLE_TOO_MANY_REDIRECTS, ErrorCode::TOO_MANY_REDIRECTS},
44+
{CURLE_UNKNOWN_OPTION, ErrorCode::UNKNOWN_OPTION},
45+
7446
// Added in curl 7.78.0.
7547
#if LIBCURL_VERSION_NUM >= 0x074E00
76-
case CURLE_SETOPT_OPTION_SYNTAX:
77-
return ErrorCode::SETOPT_OPTION_SYNTAX;
78-
#endif
79-
case CURLE_GOT_NOTHING:
80-
return ErrorCode::GOT_NOTHING;
81-
case CURLE_SSL_ENGINE_NOTFOUND:
82-
return ErrorCode::SSL_ENGINE_NOTFOUND;
83-
case CURLE_SSL_ENGINE_SETFAILED:
84-
return ErrorCode::SSL_ENGINE_SETFAILED;
85-
case CURLE_SEND_ERROR:
86-
return ErrorCode::SEND_ERROR;
87-
case CURLE_RECV_ERROR:
88-
return ErrorCode::RECV_ERROR;
89-
case CURLE_SSL_CERTPROBLEM:
90-
return ErrorCode::SSL_CERTPROBLEM;
91-
case CURLE_SSL_CIPHER:
92-
return ErrorCode::SSL_CIPHER;
93-
case CURLE_PEER_FAILED_VERIFICATION:
94-
return ErrorCode::PEER_FAILED_VERIFICATION;
95-
case CURLE_BAD_CONTENT_ENCODING:
96-
return ErrorCode::BAD_CONTENT_ENCODING;
97-
case CURLE_FILESIZE_EXCEEDED:
98-
return ErrorCode::FILESIZE_EXCEEDED;
99-
case CURLE_USE_SSL_FAILED:
100-
return ErrorCode::USE_SSL_FAILED;
101-
case CURLE_SEND_FAIL_REWIND:
102-
return ErrorCode::SEND_FAIL_REWIND;
103-
case CURLE_SSL_ENGINE_INITFAILED:
104-
return ErrorCode::SSL_ENGINE_INITFAILED;
48+
{CURLE_SETOPT_OPTION_SYNTAX, ErrorCode::SETOPT_OPTION_SYNTAX},
49+
#endif
50+
51+
{CURLE_GOT_NOTHING, ErrorCode::GOT_NOTHING},
52+
{CURLE_SSL_ENGINE_NOTFOUND, ErrorCode::SSL_ENGINE_NOTFOUND},
53+
{CURLE_SSL_ENGINE_SETFAILED, ErrorCode::SSL_ENGINE_SETFAILED},
54+
{CURLE_SEND_ERROR, ErrorCode::SEND_ERROR},
55+
{CURLE_RECV_ERROR, ErrorCode::RECV_ERROR},
56+
{CURLE_SSL_CERTPROBLEM, ErrorCode::SSL_CERTPROBLEM},
57+
{CURLE_SSL_CIPHER, ErrorCode::SSL_CIPHER},
58+
{CURLE_PEER_FAILED_VERIFICATION, ErrorCode::PEER_FAILED_VERIFICATION},
59+
{CURLE_BAD_CONTENT_ENCODING, ErrorCode::BAD_CONTENT_ENCODING},
60+
{CURLE_FILESIZE_EXCEEDED, ErrorCode::FILESIZE_EXCEEDED},
61+
{CURLE_USE_SSL_FAILED, ErrorCode::USE_SSL_FAILED},
62+
{CURLE_SEND_FAIL_REWIND, ErrorCode::SEND_FAIL_REWIND},
63+
{CURLE_SSL_ENGINE_INITFAILED, ErrorCode::SSL_ENGINE_INITFAILED},
64+
10565
// Added in curl 7.13.1.
10666
#if LIBCURL_VERSION_NUM >= 0x070D01
107-
case CURLE_LOGIN_DENIED:
108-
return ErrorCode::LOGIN_DENIED;
67+
{CURLE_LOGIN_DENIED, ErrorCode::LOGIN_DENIED},
10968
#endif
69+
11070
// Added in curl 7.16.0.
11171
#if LIBCURL_VERSION_NUM >= 0x071000
112-
case CURLE_SSL_CACERT_BADFILE:
113-
return ErrorCode::SSL_CACERT_BADFILE;
72+
{CURLE_SSL_CACERT_BADFILE, ErrorCode::SSL_CACERT_BADFILE},
11473
#endif
74+
11575
// Added in curl 7.16.1.
11676
#if LIBCURL_VERSION_NUM >= 0x071001
117-
case CURLE_SSL_SHUTDOWN_FAILED:
118-
return ErrorCode::SSL_SHUTDOWN_FAILED;
77+
{CURLE_SSL_SHUTDOWN_FAILED, ErrorCode::SSL_SHUTDOWN_FAILED},
11978
#endif
79+
12080
// Added in curl 7.18.2.
12181
#if LIBCURL_VERSION_NUM >= 0x071202
122-
case CURLE_AGAIN:
123-
return ErrorCode::AGAIN;
82+
{CURLE_AGAIN, ErrorCode::AGAIN},
12483
#endif
84+
12585
// Added in curl 7.19.0.
12686
#if LIBCURL_VERSION_NUM >= 0x071300
127-
case CURLE_SSL_CRL_BADFILE:
128-
return ErrorCode::SSL_CRL_BADFILE;
129-
case CURLE_SSL_ISSUER_ERROR:
130-
return ErrorCode::SSL_ISSUER_ERROR;
87+
{CURLE_SSL_CRL_BADFILE, ErrorCode::SSL_CRL_BADFILE},
88+
{CURLE_SSL_ISSUER_ERROR, ErrorCode::SSL_ISSUER_ERROR},
13189
#endif
90+
13291
// Added in curl 7.21.0.
13392
#if LIBCURL_VERSION_NUM >= 0x071500
134-
case CURLE_CHUNK_FAILED:
135-
return ErrorCode::CHUNK_FAILED;
93+
{CURLE_CHUNK_FAILED, ErrorCode::CHUNK_FAILED},
13694
#endif
95+
13796
// Added in curl 7.30.0.
13897
#if LIBCURL_VERSION_NUM >= 0x071E00
139-
case CURLE_NO_CONNECTION_AVAILABLE:
140-
return ErrorCode::NO_CONNECTION_AVAILABLE;
98+
{CURLE_NO_CONNECTION_AVAILABLE, ErrorCode::NO_CONNECTION_AVAILABLE},
14199
#endif
100+
142101
// Added in curl 7.39.0.
143102
#if LIBCURL_VERSION_NUM >= 0x072700
144-
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
145-
return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH;
103+
{CURLE_SSL_PINNEDPUBKEYNOTMATCH, ErrorCode::SSL_PINNEDPUBKEYNOTMATCH},
146104
#endif
105+
147106
// Added in curl 7.41.0.
148107
#if LIBCURL_VERSION_NUM >= 0x072900
149-
case CURLE_SSL_INVALIDCERTSTATUS:
150-
return ErrorCode::SSL_INVALIDCERTSTATUS;
108+
{CURLE_SSL_INVALIDCERTSTATUS, ErrorCode::SSL_INVALIDCERTSTATUS},
151109
#endif
110+
152111
// Added in curl 7.49.0.
153112
#if LIBCURL_VERSION_NUM >= 0x073100
154-
case CURLE_HTTP2_STREAM:
155-
return ErrorCode::HTTP2_STREAM;
113+
{CURLE_HTTP2_STREAM, ErrorCode::HTTP2_STREAM},
156114
#endif
115+
116+
{CURLE_PARTIAL_FILE, ErrorCode::PARTIAL_FILE},
117+
157118
// Added in curl 7.59.0.
158119
#if LIBCURL_VERSION_NUM >= 0x073B00
159-
case CURLE_RECURSIVE_API_CALL:
160-
return ErrorCode::RECURSIVE_API_CALL;
120+
{CURLE_RECURSIVE_API_CALL, ErrorCode::RECURSIVE_API_CALL},
161121
#endif
122+
162123
// Added in curl 7.66.0.
163124
#if LIBCURL_VERSION_NUM >= 0x074200
164-
case CURLE_AUTH_ERROR:
165-
return ErrorCode::AUTH_ERROR;
125+
{CURLE_AUTH_ERROR, ErrorCode::AUTH_ERROR},
166126
#endif
127+
167128
// Added in curl 7.68.0.
168129
#if LIBCURL_VERSION_NUM >= 0x074400
169-
case CURLE_HTTP3:
170-
return ErrorCode::HTTP3;
130+
{CURLE_HTTP3, ErrorCode::HTTP3},
171131
#endif
132+
172133
// Added in curl 7.69.0.
173134
#if LIBCURL_VERSION_NUM >= 0x074500
174-
case CURLE_QUIC_CONNECT_ERROR:
175-
return ErrorCode::QUIC_CONNECT_ERROR;
135+
{CURLE_QUIC_CONNECT_ERROR, ErrorCode::QUIC_CONNECT_ERROR},
176136
#endif
137+
177138
// Added in curl 7.73.0.
178139
#if LIBCURL_VERSION_NUM >= 0x074900
179-
case CURLE_PROXY:
180-
return ErrorCode::PROXY;
140+
{CURLE_PROXY, ErrorCode::PROXY},
181141
#endif
142+
182143
// Added in curl 7.77.0.
183144
#if LIBCURL_VERSION_NUM >= 0x074D00
184-
case CURLE_SSL_CLIENTCERT:
185-
return ErrorCode::SSL_CLIENTCERT;
145+
{CURLE_SSL_CLIENTCERT, ErrorCode::SSL_CLIENTCERT},
186146
#endif
147+
187148
// Added in curl 7.84.0.
188149
#if LIBCURL_VERSION_NUM >= 0x075400
189-
case CURLE_UNRECOVERABLE_POLL:
190-
return ErrorCode::UNRECOVERABLE_POLL;
150+
{CURLE_UNRECOVERABLE_POLL, ErrorCode::UNRECOVERABLE_POLL},
191151
#endif
152+
192153
// Added in curl 7.6.0.
193154
#if LIBCURL_VERSION_NUM >= 0x080600
194-
case CURLE_TOO_LARGE:
195-
return ErrorCode::TOO_LARGE;
155+
{CURLE_TOO_LARGE, ErrorCode::TOO_LARGE},
196156
#endif
197-
default:
198-
return ErrorCode::UNKNOWN_ERROR;
157+
};
158+
159+
ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
160+
auto it = curl_error_map.find(curl_code);
161+
if (it == curl_error_map.end()) {
162+
// Default return value when the CURL error code is not recognized
163+
return ErrorCode::UNKNOWN_ERROR;
199164
}
165+
return it->second;
200166
}
201-
202-
} // namespace cpr
167+
} // namespace cpr

include/cpr/error.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ enum class ErrorCode {
4545
ABORTED_BY_CALLBACK,
4646
BAD_FUNCTION_ARGUMENT,
4747
INTERFACE_FAILED,
48-
OBSOLETE46,
4948
TOO_MANY_REDIRECTS,
5049
UNKNOWN_OPTION,
5150
SETOPT_OPTION_SYNTAX,

0 commit comments

Comments
 (0)