Retry upload on XML error instead of crashing

Sometimes the Google Drive API sends malformed XML which crashes grive.
This patch adds a simple try-catch to Resource::Upload that retries the
upload if an XML exception is thrown from AuthAgent::Put.
pull/40/head
Visa Putkinen 2013-11-25 00:14:29 +02:00
parent 84785ec473
commit 3775572f46
1 changed files with 30 additions and 6 deletions

View File

@ -607,11 +607,23 @@ bool Resource::Upload(
os::Sleep( 5 ); os::Sleep( 5 );
} }
http::StringResponse str ; try {
if ( post ) http::StringResponse str ;
http->Post( link, meta, &str, hdr ) ; if ( post )
else http->Post( link, meta, &str, hdr ) ;
http->Put( link, meta, &str, hdr ) ; else
http->Put( link, meta, &str, hdr ) ;
} catch ( Error &e ) {
std::string const *info = boost::get_error_info<xml::TreeBuilder::ExpatApiError>(e);
if ( info && (*info == "XML_Parse") ) {
Log( "Error parsing pre-upload response XML, retrying whole upload in 5s",
log::warning );
retrying = true;
continue;
} else {
throw e;
}
}
http::Header uphdr ; http::Header uphdr ;
uphdr.Add( "Expect:" ) ; uphdr.Add( "Expect:" ) ;
@ -622,7 +634,19 @@ bool Resource::Upload(
http::XmlResponse xml ; http::XmlResponse xml ;
long http_code = 0; long http_code = 0;
http_code = http->Put( uplink, &file, &xml, uphdr ) ; try {
http_code = http->Put( uplink, &file, &xml, uphdr ) ;
} catch ( Error &e ) {
std::string const *info = boost::get_error_info<xml::TreeBuilder::ExpatApiError>(e);
if ( info && (*info == "XML_Parse") ) {
Log( "Error parsing response XML, retrying whole upload in 5s",
log::warning );
retrying = true;
continue;
} else {
throw e;
}
}
if ( http_code == 410 || http_code == 412 ) { if ( http_code == 410 || http_code == 412 ) {
Log( "request failed with %1%, retrying whole upload in 5s", http_code, Log( "request failed with %1%, retrying whole upload in 5s", http_code,