-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from DEGoodmanWilson/master
Adding error objects to request responses
- Loading branch information
Showing
19 changed files
with
446 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "error.h" | ||
#include <curl/curl.h> | ||
|
||
namespace cpr { | ||
|
||
ErrorCode getErrorCodeForCurlError(int curlCode) { | ||
switch(curlCode) { | ||
case CURLE_OK: return ErrorCode::OK; | ||
|
||
case CURLE_UNSUPPORTED_PROTOCOL: return ErrorCode::UNSUPPORTED_PROTOCOL; | ||
case CURLE_URL_MALFORMAT: return ErrorCode::INVALID_URL_FORMAT; | ||
case CURLE_COULDNT_RESOLVE_PROXY: return ErrorCode::PROXY_RESOLUTION_FAILURE; | ||
case CURLE_COULDNT_RESOLVE_HOST: return ErrorCode::HOST_RESOLUTION_FAILURE; | ||
case CURLE_COULDNT_CONNECT: return ErrorCode::CONNECTION_FAILURE; | ||
case CURLE_OPERATION_TIMEDOUT: return ErrorCode::OPERATION_TIMEDOUT; | ||
case CURLE_SSL_CONNECT_ERROR: return ErrorCode::SSL_CONNECT_ERROR; | ||
case CURLE_PEER_FAILED_VERIFICATION: return ErrorCode::SSL_REMOTE_CERTIFICATE_ERROR; | ||
case CURLE_GOT_NOTHING: return ErrorCode::EMPTY_RESPONSE; | ||
case CURLE_SSL_ENGINE_NOTFOUND: return ErrorCode::GENERIC_SSL_ERROR; | ||
case CURLE_SSL_ENGINE_SETFAILED: return ErrorCode::GENERIC_SSL_ERROR; | ||
case CURLE_SEND_ERROR: return ErrorCode::NETWORK_SEND_FAILURE; | ||
case CURLE_RECV_ERROR: return ErrorCode::NETWORK_RECEIVE_ERROR; | ||
case CURLE_SSL_CERTPROBLEM: return ErrorCode::SSL_LOCAL_CERTIFICATE_ERROR; | ||
case CURLE_SSL_CIPHER: return ErrorCode::GENERIC_SSL_ERROR; | ||
case CURLE_SSL_CACERT: return ErrorCode::SSL_CACERT_ERROR; | ||
case CURLE_USE_SSL_FAILED: return ErrorCode::GENERIC_SSL_ERROR; | ||
case CURLE_SSL_ENGINE_INITFAILED: return ErrorCode::GENERIC_SSL_ERROR; | ||
case CURLE_SSL_CACERT_BADFILE: return ErrorCode::SSL_CACERT_ERROR; | ||
case CURLE_SSL_SHUTDOWN_FAILED: return ErrorCode::GENERIC_SSL_ERROR; | ||
case CURLE_SSL_CRL_BADFILE: return ErrorCode::SSL_CACERT_ERROR; | ||
case CURLE_SSL_ISSUER_ERROR: return ErrorCode::SSL_CACERT_ERROR; | ||
|
||
//Degenerate errors that are already being handled in application-level code in other ways | ||
case CURLE_TOO_MANY_REDIRECTS: return ErrorCode::OK; | ||
|
||
default: return ErrorCode::INTERNAL_ERROR; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef CPR_ERROR_H | ||
#define CPR_ERROR_H | ||
|
||
#include <string> | ||
|
||
#include "cprtypes.h" | ||
#include "defines.h" | ||
|
||
namespace cpr { | ||
|
||
enum class ErrorCode { | ||
OK = 0, | ||
CONNECTION_FAILURE, | ||
EMPTY_RESPONSE, | ||
HOST_RESOLUTION_FAILURE, | ||
INTERNAL_ERROR, | ||
INVALID_URL_FORMAT, | ||
NETWORK_RECEIVE_ERROR, | ||
NETWORK_SEND_FAILURE, | ||
OPERATION_TIMEDOUT, | ||
PROXY_RESOLUTION_FAILURE, | ||
SSL_CONNECT_ERROR, | ||
SSL_LOCAL_CERTIFICATE_ERROR, | ||
SSL_REMOTE_CERTIFICATE_ERROR, | ||
SSL_CACERT_ERROR, | ||
GENERIC_SSL_ERROR, | ||
UNSUPPORTED_PROTOCOL, | ||
UNKNOWN_ERROR = 1000, | ||
}; | ||
|
||
ErrorCode getErrorCodeForCurlError(int curlCode); //int so we don't have to include curl.h | ||
|
||
class Error { | ||
public: | ||
Error() | ||
: code{ErrorCode::OK}, message{""} {} | ||
|
||
template <typename ErrorCodeType, typename TextType> | ||
Error(ErrorCode& p_error_code, TextType&& p_error_message) | ||
: code{p_error_code}, message{CPR_FWD(p_error_message)} {} | ||
|
||
|
||
ErrorCode code; | ||
std::string message; | ||
|
||
//allow easy checking of errors with: | ||
// if(error) { do something; } | ||
explicit operator bool() const { | ||
return code != ErrorCode::OK; | ||
} | ||
}; | ||
|
||
} // namespace cpr | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
|
||
#include <cpr.h> | ||
#include <curl/curl.h> | ||
|
||
|
||
#include "server.h" | ||
|
||
using namespace cpr; | ||
|
||
static Server* server = new Server(); | ||
auto base = server->GetBaseUrl(); | ||
auto baseSSL = server->GetBaseUrlSSL(); | ||
|
||
TEST(ErrorTests, BasicSSLFailure) { | ||
//If CURL has SSL enabled, we should expect SSL_CONECT_ERROR | ||
// If not, we shoujld expect UNSUPPORTED_PROTOCOL | ||
|
||
|
||
auto url = Url{baseSSL + "/hello.html"}; | ||
auto response = cpr::Get(url); | ||
EXPECT_EQ(url, response.url); | ||
EXPECT_EQ(0, response.status_code); | ||
|
||
//This is ugly, because it presume CURL under the hood. Might be nice to have something in `cpr::Session` | ||
// That could tell us if SSL is supported. | ||
auto curl_version = curl_version_info(CURLVERSION_NOW); | ||
auto expected = ErrorCode::UNSUPPORTED_PROTOCOL; | ||
if(curl_version->features & CURL_VERSION_SSL) { | ||
expected = ErrorCode::SSL_CONNECT_ERROR; | ||
} | ||
EXPECT_EQ(expected, response.error.code); | ||
|
||
} | ||
|
||
//Not terribly sure how to test other SSL error codes | ||
|
||
TEST(ErrorTests, UnsupportedProtocolFailure) { | ||
auto url = Url{"urk://wat.is.this"}; | ||
auto response = cpr::Get(url); | ||
EXPECT_EQ(0, response.status_code); | ||
EXPECT_EQ(ErrorCode::UNSUPPORTED_PROTOCOL, response.error.code); | ||
} | ||
|
||
TEST(ErrorTests, InvalidURLFailure) { | ||
auto url = Url{"???"}; | ||
auto response = cpr::Get(url); | ||
EXPECT_EQ(0, response.status_code); | ||
EXPECT_EQ(ErrorCode::INVALID_URL_FORMAT, response.error.code); | ||
} | ||
|
||
TEST(ErrorTests, TimeoutFailure) { | ||
auto url = Url{"http://railstars.com"}; //my own site, and notoriously slow to load. Need a better exemplar | ||
auto response = cpr::Get(url, cpr::Timeout{1}); | ||
EXPECT_EQ(0, response.status_code); | ||
EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code); | ||
} | ||
|
||
TEST(ErrorTests, ProxyFailure) { | ||
auto url = Url{base + "/hello.html"}; | ||
auto response = cpr::Get(url, cpr::Proxies{{"http", "http://bad_host/"}}); | ||
EXPECT_EQ(url, response.url); | ||
EXPECT_EQ(0, response.status_code); | ||
EXPECT_EQ(ErrorCode::PROXY_RESOLUTION_FAILURE, response.error.code); | ||
} | ||
|
||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
::testing::AddGlobalTestEnvironment(server); | ||
return RUN_ALL_TESTS(); | ||
} |
Oops, something went wrong.