fix: Raise meaningful exception when oauth callback times out (#363) · googleapis/google-cloud-python@adc3ee6
@@ -410,8 +410,9 @@ def run_local_server(
410410 in the user's browser.
411411 redirect_uri_trailing_slash (bool): whether or not to add trailing
412412 slash when constructing the redirect_uri. Default value is True.
413- timeout_seconds (int): It will raise an error after the timeout timing
414- if there are no credentials response. The value is in seconds.
413+ timeout_seconds (int): It will raise a WSGITimeoutError exception after the
414+ timeout timing if there are no credentials response. The value is in
415+ seconds.
415416 When set to None there is no timeout.
416417 Default value is None.
417418 token_audience (str): Passed along with the request for an access
@@ -425,6 +426,10 @@ def run_local_server(
425426 Returns:
426427 google.oauth2.credentials.Credentials: The OAuth 2.0 credentials
427428 for the user.
429+430+ Raises:
431+ WSGITimeoutError: If there is a timeout when waiting for the response from the
432+ authorization server.
428433 """
429434wsgi_app = _RedirectWSGIApp(success_message)
430435# Fail fast if the address is occupied
@@ -455,7 +460,15 @@ def run_local_server(
455460456461# Note: using https here because oauthlib is very picky that
457462# OAuth 2.0 should only occur over https.
458-authorization_response = wsgi_app.last_request_uri.replace("http", "https")
463+try:
464+authorization_response = wsgi_app.last_request_uri.replace(
465+"http", "https"
466+ )
467+except AttributeError as e:
468+raise WSGITimeoutError(
469+"Timed out waiting for response from authorization server"
470+ ) from e
471+459472self.fetch_token(
460473authorization_response=authorization_response, audience=token_audience
461474 )
@@ -506,3 +519,7 @@ def __call__(self, environ, start_response):
506519start_response("200 OK", [("Content-type", "text/plain; charset=utf-8")])
507520self.last_request_uri = wsgiref.util.request_uri(environ)
508521return [self._success_message.encode("utf-8")]
522+523+524+class WSGITimeoutError(AttributeError):
525+"""Raised when the WSGI server times out waiting for a response."""