don't throw in CurlAgent for all HTTP errors (#82)

pull/40/head
Nestal Wan 2012-07-26 01:23:20 +08:00
parent 22fd859be6
commit 5a0c6ae56e
4 changed files with 46 additions and 15 deletions

View File

@ -156,12 +156,11 @@ long CurlAgent::ExecCurl(
Trace( "HTTP response %1%", http_code ) ;
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 0 ) ;
if ( curl_code != CURLE_OK || (http_code >= 400 && http_code < 500) )
if ( curl_code != CURLE_OK )
{
BOOST_THROW_EXCEPTION(
Error()
<< CurlCode( curl_code )
<< HttpResponse( http_code )
<< Url( url )
<< expt::ErrMsg( error )
<< HttpHeader( hdr )

View File

@ -29,16 +29,16 @@ struct Error : virtual Exception {} ;
// CURL error code
typedef boost::error_info<struct CurlCodeTag, int> CurlCode ;
// HTTP response code
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;
// HTTP response body
typedef boost::error_info<struct HttpResponseStrTag, std::string> HttpResponseText ;
// URL
typedef boost::error_info<struct UrlTag, std::string> Url ;
// HTTP headers
typedef boost::error_info<struct HeaderTag, Header> HttpHeader ;
// HTTP response code
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;
// HTTP response body
typedef boost::error_info<struct HttpResponseStrTag, std::string> HttpResponseText ;
} } // end of namespace

View File

@ -19,6 +19,7 @@
#include "AuthAgent.hh"
#include "http/Error.hh"
#include "http/Header.hh"
#include "util/log/Log.hh"
#include "util/OS.hh"
@ -50,11 +51,13 @@ long AuthAgent::Put(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;
long response ;
while ( CheckRetry(
response = m_agent->Put(url, data, dest, AppendHeader(hdr)) ) ) ;
response = m_agent->Put(url, data, dest, auth) ) ) ;
return response ;
return CheckHttpResponse(response, url, auth) ;
}
long AuthAgent::Put(
@ -63,11 +66,13 @@ long AuthAgent::Put(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;
long response ;
while ( CheckRetry(
response = m_agent->Put( url, file, dest, AppendHeader(hdr) ) ) ) ;
return response ;
return CheckHttpResponse(response, url, auth) ;
}
long AuthAgent::Get(
@ -75,11 +80,13 @@ long AuthAgent::Get(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;
long response ;
while ( CheckRetry(
response = m_agent->Get( url, dest, AppendHeader(hdr) ) ) ) ;
return response ;
return CheckHttpResponse(response, url, auth) ;
}
long AuthAgent::Post(
@ -88,11 +95,13 @@ long AuthAgent::Post(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;
long response ;
while ( CheckRetry(
response = m_agent->Post( url, data, dest, AppendHeader(hdr) ) ) ) ;
return response ;
return CheckHttpResponse(response, url, auth) ;
}
long AuthAgent::Custom(
@ -101,11 +110,13 @@ long AuthAgent::Custom(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;
long response ;
while ( CheckRetry(
response = m_agent->Custom( method, url, dest, AppendHeader(hdr) ) ) ) ;
return response ;
return CheckHttpResponse(response, url, auth) ;
}
std::string AuthAgent::RedirLocation() const
@ -144,9 +155,26 @@ bool AuthAgent::CheckRetry( long response )
m_auth.Refresh() ;
return true ;
}
else
return false ;
}
long AuthAgent::CheckHttpResponse(
long response,
const std::string& url,
const http::Header& hdr )
{
// throw for other HTTP errors
if ( response >= 400 && response < 500 )
{
BOOST_THROW_EXCEPTION(
Error()
<< HttpResponse( response )
<< Url( url )
<< HttpHeader( hdr ) ) ;
}
return response ;
}
} // end of namespace

View File

@ -73,6 +73,10 @@ public :
private :
http::Header AppendHeader( const http::Header& hdr ) const ;
bool CheckRetry( long response ) ;
long CheckHttpResponse(
long response,
const std::string& url,
const http::Header& hdr ) ;
private :
OAuth2 m_auth ;