Question

Let me first provide some background information. If you don't care you can skip to the next paragraph. I wanted to use the DrEdit sample Java application which is integrated with Google Drive as the basis of my application. I need to refactor the code, though, because the original scenario assumed that the user would only access the application through the Drive and never directly. Currently when the latter happens, a RuntimeException is thrown which should not be the case in a normal flow.

Thanks to that issue I stumbled upon a difference between my local environment and the GAE which is manifested when the following code is run:

} catch (CredentialMediator.NoRefreshTokenException e) {
  try {
    resp.sendRedirect(e.getAuthorizationUrl());
  } catch (IOException ioe) {
    throw new RuntimeException("Failed to redirect user for authorization");
  }
  throw new RuntimeException("No refresh token found. Re-authorizing.");
}

When I run this application on GAE, the RuntimeException is thrown (I can see it in the logs) and the sendRedirect is also executed so I get to see the page that should be displayed.

However when I run the same application locally, I get the HTTP 500 error and the RuntimeException is displayed but the sendRedirect is ignored.

So far I haven't been successful in finding an explanation for this behaviour. I would like to know why this is the case and if there are settings that I can change in order to fully replicate the GAE environment locally.

No correct solution

OTHER TIPS

This is how standard defines the sendRedirect(). It actually commits the response so after calling this method you should not be able to change or add to the response. However it does not define what happens if you trigger an exception after redirect.

Anyway, your code is ambiguous on purpose - you should not continue processing the request and throw exceptions after sending redirect. If you have any processing to do, then do it before redirect.

OTOH you should not rely on generic exception handling. Instead install a servlet filter that catches exceptions and return a proper user-readable or device-readable response.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top