Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReportDownloader - Bad Gateway exception #41

Closed
jglapa opened this issue Apr 20, 2015 · 3 comments
Closed

ReportDownloader - Bad Gateway exception #41

jglapa opened this issue Apr 20, 2015 · 3 comments

Comments

@jglapa
Copy link

jglapa commented Apr 20, 2015

Hi, It sometimes happens that ReportDownloader throws an exception because it cannot parse the error response.

This happens when Google backend is unavailable (we're getting an 502 Error with the robot page). It seems that this code doesn't handle the situation when the error response is not correctly formatted in xml format.

Could we in catch this parsing exception and throw a Detailed/ReportDownloadResponseException with 502 error code?

example exception:

Apr 17 06:19:15 localhost utils.report_download WARN Response received with status code 502 and message: Bad Gateway
Apr 17 06:19:15 localhost utils.report_download ERROR Couldn't process XML into a Document
Apr 17 06:19:15 localhost org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 12; Open quote is expected for attribute "lang" associated with an  element type  "html".
Apr 17 06:19:15 localhost     at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
Apr 17 06:19:15 localhost     at com.google.api.ads.adwords.lib.utils.XmlFieldExtractor.getDocument(XmlFieldExtractor.java:125)
Apr 17 06:19:15 localhost     at com.google.api.ads.adwords.lib.utils.XmlFieldExtractor.extract(XmlFieldExtractor.java:81)
Apr 17 06:19:15 localhost     at com.google.api.ads.adwords.lib.utils.v201409.ReportDownloader.handleResponse(ReportDownloader.java:115)
Apr 17 06:19:15 localhost     at com.google.api.ads.adwords.lib.utils.v201409.ReportDownloader.downloadReport(ReportDownloader.java:137)
.....
Apr 17 06:19:15 localhost com.xxx.yyy.AdWordsException: null <!DOCTYPE html>
Apr 17 06:19:15 localhost <html lang=en>
Apr 17 06:19:15 localhost   <meta charset=utf-8>
Apr 17 06:19:15 localhost   <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
Apr 17 06:19:15 localhost   <title>Error 502 (Server Error)!!1</title>
Apr 17 06:19:15 localhost   <style>
Apr 17 06:19:15 localhost     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
Apr 17 06:19:15 localhost   </style>
Apr 17 06:19:15 localhost   <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
Apr 17 06:19:15 localhost   <p><b>502.</b> <ins>That~@~Ys an error.</ins>
Apr 17 06:19:15 localhost   <p>The server encountered a temporary error and could not complete your request.<p>Please try again in 30 seconds.  <ins>That~@~Ys all we know.</ins>
Apr 17 06:19:15 localhost
....

v201502 has the same problem

@jradcliff
Copy link
Member

Thanks for pointing this out. I'll take a look -- seems like we need a try/finally in the code that's parsing the exception XML, as you mentioned.

@jradcliff
Copy link
Member

On closer examination, I realized that the library does throw a ReportDownloadResponseException in this scenario. The log messages you mentioned are coming from the catch(SAXException) block in XmlFieldExtractor.extract, which logs any XML parsing issues (instead of throwing them) so that parsing issues won't mask the underlying exception.

After the XML parsing errors are logged, the ReportDownloader will proceed past the call to XmlFieldExtractor.extract and throw the DetailedReportDownloadResponseException. Below is the relevant code from ReportDownloader, with some additional comments for clarification.

    DetailedReportDownloadResponseException exception =
        new DetailedReportDownloadResponseException(response.getHttpStatus(), responseText);
    XmlFieldExtractor extractor = new XmlFieldExtractor();
    // Exceptions thrown by the call to 'extract' are logged,
    // but not rethrown.
    Map<String, String> fields = extractor.extract(
        new ByteArrayInputStream(responseText.getBytes()),
        new String[] {"fieldPath", "trigger", "type"});
    // Will still reach this point, even if an exception occurred
    // within the call to 'extract' above.
    exception.setFieldPath(fields.get("fieldPath"));
    exception.setTrigger(fields.get("trigger"));
    exception.setType(fields.get("type"));
    throw exception;

@jradcliff
Copy link
Member

As far as I can tell, this is working as intended. Please see my previous comment and reopen if you still see an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants