Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 101 additions & 136 deletions cpr/error.cpp
Original file line number Diff line number Diff line change
@@ -1,202 +1,167 @@
#include "cpr/error.h"
#include <curl/curlver.h>
#include <unordered_map>
#include <cstdint>
#include <curl/curl.h>
#include <curl/curlver.h>

namespace cpr {
ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
switch (curl_code) {
case CURLE_OK:
return ErrorCode::OK;
case CURLE_UNSUPPORTED_PROTOCOL:
return ErrorCode::UNSUPPORTED_PROTOCOL;
case CURLE_FAILED_INIT:
return ErrorCode::FAILED_INIT;
case CURLE_URL_MALFORMAT:
return ErrorCode::URL_MALFORMAT;
case CURLE_NOT_BUILT_IN:
return ErrorCode::NOT_BUILT_IN;
case CURLE_COULDNT_RESOLVE_PROXY:
return ErrorCode::COULDNT_RESOLVE_PROXY;
case CURLE_COULDNT_RESOLVE_HOST:
return ErrorCode::COULDNT_RESOLVE_HOST;
case CURLE_COULDNT_CONNECT:
return ErrorCode::COULDNT_CONNECT;
static const std::unordered_map<std::int32_t, ErrorCode> curl_error_map = { // NOLINT - (needed because of static init)
{CURLE_OK, ErrorCode::OK},
{CURLE_UNSUPPORTED_PROTOCOL, ErrorCode::UNSUPPORTED_PROTOCOL},
{CURLE_FAILED_INIT, ErrorCode::FAILED_INIT},
{CURLE_URL_MALFORMAT, ErrorCode::URL_MALFORMAT},
{CURLE_NOT_BUILT_IN, ErrorCode::NOT_BUILT_IN},
{CURLE_COULDNT_RESOLVE_PROXY, ErrorCode::COULDNT_RESOLVE_PROXY},
{CURLE_COULDNT_RESOLVE_HOST, ErrorCode::COULDNT_RESOLVE_HOST},
{CURLE_COULDNT_CONNECT, ErrorCode::COULDNT_CONNECT},

// Name changed in curl >= 7.51.0.
#if LIBCURL_VERSION_NUM >= 0x073300
case CURLE_WEIRD_SERVER_REPLY:
{CURLE_WEIRD_SERVER_REPLY, ErrorCode::WEIRD_SERVER_REPLY},
#else
case CURLE_FTP_WEIRD_SERVER_REPLY:
#endif
return ErrorCode::WEIRD_SERVER_REPLY;
case CURLE_REMOTE_ACCESS_DENIED:
return ErrorCode::REMOTE_ACCESS_DENIED;
case CURLE_HTTP2:
return ErrorCode::HTTP2;
case CURLE_QUOTE_ERROR:
return ErrorCode::QUOTE_ERROR;
case CURLE_HTTP_RETURNED_ERROR:
return ErrorCode::HTTP_RETURNED_ERROR;
case CURLE_WRITE_ERROR:
return ErrorCode::WRITE_ERROR;
case CURLE_UPLOAD_FAILED:
return ErrorCode::UPLOAD_FAILED;
case CURLE_READ_ERROR:
return ErrorCode::READ_ERROR;
case CURLE_OUT_OF_MEMORY:
return ErrorCode::OUT_OF_MEMORY;
case CURLE_OPERATION_TIMEDOUT:
return ErrorCode::OPERATION_TIMEDOUT;
case CURLE_RANGE_ERROR:
return ErrorCode::RANGE_ERROR;
case CURLE_HTTP_POST_ERROR:
return ErrorCode::HTTP_POST_ERROR;
case CURLE_SSL_CONNECT_ERROR:
return ErrorCode::SSL_CONNECT_ERROR;
case CURLE_BAD_DOWNLOAD_RESUME:
return ErrorCode::BAD_DOWNLOAD_RESUME;
case CURLE_FILE_COULDNT_READ_FILE:
return ErrorCode::FILE_COULDNT_READ_FILE;
case CURLE_FUNCTION_NOT_FOUND:
return ErrorCode::FUNCTION_NOT_FOUND;
case CURLE_ABORTED_BY_CALLBACK:
return ErrorCode::ABORTED_BY_CALLBACK;
case CURLE_BAD_FUNCTION_ARGUMENT:
return ErrorCode::BAD_FUNCTION_ARGUMENT;
case CURLE_INTERFACE_FAILED:
return ErrorCode::INTERFACE_FAILED;
case CURLE_OBSOLETE46:
return ErrorCode::OBSOLETE46;
case CURLE_TOO_MANY_REDIRECTS:
return ErrorCode::TOO_MANY_REDIRECTS;
case CURLE_UNKNOWN_OPTION:
return ErrorCode::UNKNOWN_OPTION;
{CURLE_FTP_WEIRD_SERVER_REPLY, ErrorCode::WEIRD_SERVER_REPLY},
#endif

{CURLE_REMOTE_ACCESS_DENIED, ErrorCode::REMOTE_ACCESS_DENIED},
{CURLE_HTTP2, ErrorCode::HTTP2},
{CURLE_QUOTE_ERROR, ErrorCode::QUOTE_ERROR},
{CURLE_HTTP_RETURNED_ERROR, ErrorCode::HTTP_RETURNED_ERROR},
{CURLE_WRITE_ERROR, ErrorCode::WRITE_ERROR},
{CURLE_UPLOAD_FAILED, ErrorCode::UPLOAD_FAILED},
{CURLE_READ_ERROR, ErrorCode::READ_ERROR},
{CURLE_OUT_OF_MEMORY, ErrorCode::OUT_OF_MEMORY},
{CURLE_OPERATION_TIMEDOUT, ErrorCode::OPERATION_TIMEDOUT},
{CURLE_RANGE_ERROR, ErrorCode::RANGE_ERROR},
{CURLE_HTTP_POST_ERROR, ErrorCode::HTTP_POST_ERROR},
{CURLE_SSL_CONNECT_ERROR, ErrorCode::SSL_CONNECT_ERROR},
{CURLE_BAD_DOWNLOAD_RESUME, ErrorCode::BAD_DOWNLOAD_RESUME},
{CURLE_FILE_COULDNT_READ_FILE, ErrorCode::FILE_COULDNT_READ_FILE},
{CURLE_FUNCTION_NOT_FOUND, ErrorCode::FUNCTION_NOT_FOUND},
{CURLE_ABORTED_BY_CALLBACK, ErrorCode::ABORTED_BY_CALLBACK},
{CURLE_BAD_FUNCTION_ARGUMENT, ErrorCode::BAD_FUNCTION_ARGUMENT},
{CURLE_INTERFACE_FAILED, ErrorCode::INTERFACE_FAILED},
{CURLE_TOO_MANY_REDIRECTS, ErrorCode::TOO_MANY_REDIRECTS},
{CURLE_UNKNOWN_OPTION, ErrorCode::UNKNOWN_OPTION},

// Added in curl 7.78.0.
#if LIBCURL_VERSION_NUM >= 0x074E00
case CURLE_SETOPT_OPTION_SYNTAX:
return ErrorCode::SETOPT_OPTION_SYNTAX;
#endif
case CURLE_GOT_NOTHING:
return ErrorCode::GOT_NOTHING;
case CURLE_SSL_ENGINE_NOTFOUND:
return ErrorCode::SSL_ENGINE_NOTFOUND;
case CURLE_SSL_ENGINE_SETFAILED:
return ErrorCode::SSL_ENGINE_SETFAILED;
case CURLE_SEND_ERROR:
return ErrorCode::SEND_ERROR;
case CURLE_RECV_ERROR:
return ErrorCode::RECV_ERROR;
case CURLE_SSL_CERTPROBLEM:
return ErrorCode::SSL_CERTPROBLEM;
case CURLE_SSL_CIPHER:
return ErrorCode::SSL_CIPHER;
case CURLE_PEER_FAILED_VERIFICATION:
return ErrorCode::PEER_FAILED_VERIFICATION;
case CURLE_BAD_CONTENT_ENCODING:
return ErrorCode::BAD_CONTENT_ENCODING;
case CURLE_FILESIZE_EXCEEDED:
return ErrorCode::FILESIZE_EXCEEDED;
case CURLE_USE_SSL_FAILED:
return ErrorCode::USE_SSL_FAILED;
case CURLE_SEND_FAIL_REWIND:
return ErrorCode::SEND_FAIL_REWIND;
case CURLE_SSL_ENGINE_INITFAILED:
return ErrorCode::SSL_ENGINE_INITFAILED;
{CURLE_SETOPT_OPTION_SYNTAX, ErrorCode::SETOPT_OPTION_SYNTAX},
#endif

{CURLE_GOT_NOTHING, ErrorCode::GOT_NOTHING},
{CURLE_SSL_ENGINE_NOTFOUND, ErrorCode::SSL_ENGINE_NOTFOUND},
{CURLE_SSL_ENGINE_SETFAILED, ErrorCode::SSL_ENGINE_SETFAILED},
{CURLE_SEND_ERROR, ErrorCode::SEND_ERROR},
{CURLE_RECV_ERROR, ErrorCode::RECV_ERROR},
{CURLE_SSL_CERTPROBLEM, ErrorCode::SSL_CERTPROBLEM},
{CURLE_SSL_CIPHER, ErrorCode::SSL_CIPHER},
{CURLE_PEER_FAILED_VERIFICATION, ErrorCode::PEER_FAILED_VERIFICATION},
{CURLE_BAD_CONTENT_ENCODING, ErrorCode::BAD_CONTENT_ENCODING},
{CURLE_FILESIZE_EXCEEDED, ErrorCode::FILESIZE_EXCEEDED},
{CURLE_USE_SSL_FAILED, ErrorCode::USE_SSL_FAILED},
{CURLE_SEND_FAIL_REWIND, ErrorCode::SEND_FAIL_REWIND},
{CURLE_SSL_ENGINE_INITFAILED, ErrorCode::SSL_ENGINE_INITFAILED},

// Added in curl 7.13.1.
#if LIBCURL_VERSION_NUM >= 0x070D01
case CURLE_LOGIN_DENIED:
return ErrorCode::LOGIN_DENIED;
{CURLE_LOGIN_DENIED, ErrorCode::LOGIN_DENIED},
#endif

// Added in curl 7.16.0.
#if LIBCURL_VERSION_NUM >= 0x071000
case CURLE_SSL_CACERT_BADFILE:
return ErrorCode::SSL_CACERT_BADFILE;
{CURLE_SSL_CACERT_BADFILE, ErrorCode::SSL_CACERT_BADFILE},
#endif

// Added in curl 7.16.1.
#if LIBCURL_VERSION_NUM >= 0x071001
case CURLE_SSL_SHUTDOWN_FAILED:
return ErrorCode::SSL_SHUTDOWN_FAILED;
{CURLE_SSL_SHUTDOWN_FAILED, ErrorCode::SSL_SHUTDOWN_FAILED},
#endif

// Added in curl 7.18.2.
#if LIBCURL_VERSION_NUM >= 0x071202
case CURLE_AGAIN:
return ErrorCode::AGAIN;
{CURLE_AGAIN, ErrorCode::AGAIN},
#endif

// Added in curl 7.19.0.
#if LIBCURL_VERSION_NUM >= 0x071300
case CURLE_SSL_CRL_BADFILE:
return ErrorCode::SSL_CRL_BADFILE;
case CURLE_SSL_ISSUER_ERROR:
return ErrorCode::SSL_ISSUER_ERROR;
{CURLE_SSL_CRL_BADFILE, ErrorCode::SSL_CRL_BADFILE},
{CURLE_SSL_ISSUER_ERROR, ErrorCode::SSL_ISSUER_ERROR},
#endif

// Added in curl 7.21.0.
#if LIBCURL_VERSION_NUM >= 0x071500
case CURLE_CHUNK_FAILED:
return ErrorCode::CHUNK_FAILED;
{CURLE_CHUNK_FAILED, ErrorCode::CHUNK_FAILED},
#endif

// Added in curl 7.30.0.
#if LIBCURL_VERSION_NUM >= 0x071E00
case CURLE_NO_CONNECTION_AVAILABLE:
return ErrorCode::NO_CONNECTION_AVAILABLE;
{CURLE_NO_CONNECTION_AVAILABLE, ErrorCode::NO_CONNECTION_AVAILABLE},
#endif

// Added in curl 7.39.0.
#if LIBCURL_VERSION_NUM >= 0x072700
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH;
{CURLE_SSL_PINNEDPUBKEYNOTMATCH, ErrorCode::SSL_PINNEDPUBKEYNOTMATCH},
#endif

// Added in curl 7.41.0.
#if LIBCURL_VERSION_NUM >= 0x072900
case CURLE_SSL_INVALIDCERTSTATUS:
return ErrorCode::SSL_INVALIDCERTSTATUS;
{CURLE_SSL_INVALIDCERTSTATUS, ErrorCode::SSL_INVALIDCERTSTATUS},
#endif

// Added in curl 7.49.0.
#if LIBCURL_VERSION_NUM >= 0x073100
case CURLE_HTTP2_STREAM:
return ErrorCode::HTTP2_STREAM;
{CURLE_HTTP2_STREAM, ErrorCode::HTTP2_STREAM},
#endif

{CURLE_PARTIAL_FILE, ErrorCode::PARTIAL_FILE},

// Added in curl 7.59.0.
#if LIBCURL_VERSION_NUM >= 0x073B00
case CURLE_RECURSIVE_API_CALL:
return ErrorCode::RECURSIVE_API_CALL;
{CURLE_RECURSIVE_API_CALL, ErrorCode::RECURSIVE_API_CALL},
#endif

// Added in curl 7.66.0.
#if LIBCURL_VERSION_NUM >= 0x074200
case CURLE_AUTH_ERROR:
return ErrorCode::AUTH_ERROR;
{CURLE_AUTH_ERROR, ErrorCode::AUTH_ERROR},
#endif

// Added in curl 7.68.0.
#if LIBCURL_VERSION_NUM >= 0x074400
case CURLE_HTTP3:
return ErrorCode::HTTP3;
{CURLE_HTTP3, ErrorCode::HTTP3},
#endif

// Added in curl 7.69.0.
#if LIBCURL_VERSION_NUM >= 0x074500
case CURLE_QUIC_CONNECT_ERROR:
return ErrorCode::QUIC_CONNECT_ERROR;
{CURLE_QUIC_CONNECT_ERROR, ErrorCode::QUIC_CONNECT_ERROR},
#endif

// Added in curl 7.73.0.
#if LIBCURL_VERSION_NUM >= 0x074900
case CURLE_PROXY:
return ErrorCode::PROXY;
{CURLE_PROXY, ErrorCode::PROXY},
#endif

// Added in curl 7.77.0.
#if LIBCURL_VERSION_NUM >= 0x074D00
case CURLE_SSL_CLIENTCERT:
return ErrorCode::SSL_CLIENTCERT;
{CURLE_SSL_CLIENTCERT, ErrorCode::SSL_CLIENTCERT},
#endif

// Added in curl 7.84.0.
#if LIBCURL_VERSION_NUM >= 0x075400
case CURLE_UNRECOVERABLE_POLL:
return ErrorCode::UNRECOVERABLE_POLL;
{CURLE_UNRECOVERABLE_POLL, ErrorCode::UNRECOVERABLE_POLL},
#endif

// Added in curl 7.6.0.
#if LIBCURL_VERSION_NUM >= 0x080600
case CURLE_TOO_LARGE:
return ErrorCode::TOO_LARGE;
{CURLE_TOO_LARGE, ErrorCode::TOO_LARGE},
#endif
default:
return ErrorCode::UNKNOWN_ERROR;
};

ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
auto it = curl_error_map.find(curl_code);
if (it == curl_error_map.end()) {
// Default return value when the CURL error code is not recognized
return ErrorCode::UNKNOWN_ERROR;
}
return it->second;
}

} // namespace cpr
} // namespace cpr
1 change: 0 additions & 1 deletion include/cpr/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ enum class ErrorCode {
ABORTED_BY_CALLBACK,
BAD_FUNCTION_ARGUMENT,
INTERFACE_FAILED,
OBSOLETE46,
TOO_MANY_REDIRECTS,
UNKNOWN_OPTION,
SETOPT_OPTION_SYNTAX,
Expand Down