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 ) ; Trace( "HTTP response %1%", http_code ) ;
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 0 ) ; ::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( BOOST_THROW_EXCEPTION(
Error() Error()
<< CurlCode( curl_code ) << CurlCode( curl_code )
<< HttpResponse( http_code )
<< Url( url ) << Url( url )
<< expt::ErrMsg( error ) << expt::ErrMsg( error )
<< HttpHeader( hdr ) << HttpHeader( hdr )

View File

@ -29,16 +29,16 @@ struct Error : virtual Exception {} ;
// CURL error code // CURL error code
typedef boost::error_info<struct CurlCodeTag, int> CurlCode ; 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 // URL
typedef boost::error_info<struct UrlTag, std::string> Url ; typedef boost::error_info<struct UrlTag, std::string> Url ;
// HTTP headers // HTTP headers
typedef boost::error_info<struct HeaderTag, Header> HttpHeader ; 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 } } // end of namespace

View File

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

View File

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