diff --git a/appengine/standard_python3/bundled-services/blobstore/django/main.py b/appengine/standard_python3/bundled-services/blobstore/django/main.py index 8587852c9935..3840bc5ec479 100644 --- a/appengine/standard_python3/bundled-services/blobstore/django/main.py +++ b/appengine/standard_python3/bundled-services/blobstore/django/main.py @@ -56,6 +56,8 @@ def get(self, environ, photo_key): # GAE sets it to a guessed type if the header is not set. response["Content-Type"] = None return response + + # [END gae_blobstore_handler_django] diff --git a/appengine/standard_python3/bundled-services/blobstore/flask/main.py b/appengine/standard_python3/bundled-services/blobstore/flask/main.py index 019cb5c45b15..ee1b888504c1 100644 --- a/appengine/standard_python3/bundled-services/blobstore/flask/main.py +++ b/appengine/standard_python3/bundled-services/blobstore/flask/main.py @@ -59,6 +59,8 @@ def view_photo(photo_key): def upload_photo(): """Upload handler called by blobstore when a blob is uploaded in the test.""" return PhotoUploadHandler().post() + + # [END gae_blobstore_handler_flask] diff --git a/appengine/standard_python3/bundled-services/deferred/flask/main.py b/appengine/standard_python3/bundled-services/deferred/flask/main.py index 14be2d8760ab..5ed96bcf21e3 100644 --- a/appengine/standard_python3/bundled-services/deferred/flask/main.py +++ b/appengine/standard_python3/bundled-services/deferred/flask/main.py @@ -61,4 +61,6 @@ def custom_deferred(): print("Executing deferred task.") # request.environ contains the WSGI `environ` dictionary (See PEP 0333) return deferred.Handler().post(request.environ) + + # [END gae_deferred_handler_flask] diff --git a/appengine/standard_python3/bundled-services/mail/django/main.py b/appengine/standard_python3/bundled-services/mail/django/main.py index 89f7ff27d771..05ba58e2d7e0 100644 --- a/appengine/standard_python3/bundled-services/mail/django/main.py +++ b/appengine/standard_python3/bundled-services/mail/django/main.py @@ -58,12 +58,19 @@ def send_mail(address, body): if address is None: return HttpResponse(content="Error: Missing email address", status=400) - mail.send_mail( - sender=f"demo-app@{project_id}.appspotmail.com", - to=address, - subject="App Engine Outgoing Email", - body=body, - ) + try: + mail.send_mail( + sender=f"demo-app@{project_id}.appspotmail.com", + to=address, + subject="App Engine Outgoing Email", + body=body, + ) + except Exception as e: + print(f"Sending mail to {address} failed with exception {e}.") + return HttpResponse( + content=f"Exception {e} when sending mail to {address}.", + status=500, + ) print(f"Successfully sent mail to {address}.") return HttpResponse(content=f"Successfully sent mail to {address}.", status=201) @@ -79,6 +86,8 @@ def receive_mail(request): break return HttpResponse("OK") + + # [END gae_mail_handler_receive_django] @@ -91,6 +100,8 @@ def receive_bounce(request): print(f"Bounce notification: {bounce_message.notification}") return HttpResponse("OK") + + # [END gae_mail_handler_bounce_django] diff --git a/appengine/standard_python3/bundled-services/mail/django/main_test.py b/appengine/standard_python3/bundled-services/mail/django/main_test.py index d0026eebf7c0..12eb5c211727 100644 --- a/appengine/standard_python3/bundled-services/mail/django/main_test.py +++ b/appengine/standard_python3/bundled-services/mail/django/main_test.py @@ -90,7 +90,7 @@ def test_send_receive(version): assert "Successfully sent mail" in response.text # Give the mail some time to be delivered and logs to post - time.sleep(30) + time.sleep(60) # Fetch logs to check messages on received mail entries = gcloud_cli( diff --git a/appengine/standard_python3/bundled-services/mail/flask/main.py b/appengine/standard_python3/bundled-services/mail/flask/main.py index 6b87eb2f1d64..86735bc4540d 100644 --- a/appengine/standard_python3/bundled-services/mail/flask/main.py +++ b/appengine/standard_python3/bundled-services/mail/flask/main.py @@ -56,12 +56,16 @@ def send_mail(): print("Error: missing email address") return "Error: Missing email address", 400 - mail.send_mail( - sender=f"demo-app@{project_id}.appspotmail.com", - to=address, - subject="App Engine Outgoing Email", - body=request.form.get("body"), - ) + try: + mail.send_mail( + sender=f"demo-app@{project_id}.appspotmail.com", + to=address, + subject="App Engine Outgoing Email", + body=request.form.get("body"), + ) + except Exception as e: + print(f"Sending mail to {address} failed with exception {e}.") + return f"Exception {e} when sending mail to {address}.", 500 print(f"Successfully sent mail to {address}.") return f"Successfully sent mail to {address}.", 201 @@ -77,6 +81,8 @@ def receive_bounce(): print("Bounce notification: ", bounce_message.notification) return "OK", 200 + + # [END gae_mail_handler_bounce_flask] @@ -92,4 +98,6 @@ def receive_mail(path): break return "OK", 200 + + # [END gae_mail_handler_receive_flask] diff --git a/appengine/standard_python3/bundled-services/mail/flask/main_test.py b/appengine/standard_python3/bundled-services/mail/flask/main_test.py index d0026eebf7c0..12eb5c211727 100644 --- a/appengine/standard_python3/bundled-services/mail/flask/main_test.py +++ b/appengine/standard_python3/bundled-services/mail/flask/main_test.py @@ -90,7 +90,7 @@ def test_send_receive(version): assert "Successfully sent mail" in response.text # Give the mail some time to be delivered and logs to post - time.sleep(30) + time.sleep(60) # Fetch logs to check messages on received mail entries = gcloud_cli( diff --git a/appengine/standard_python3/bundled-services/mail/wsgi/main.py b/appengine/standard_python3/bundled-services/mail/wsgi/main.py index 0e6058117ac0..51ccecceda06 100644 --- a/appengine/standard_python3/bundled-services/mail/wsgi/main.py +++ b/appengine/standard_python3/bundled-services/mail/wsgi/main.py @@ -36,6 +36,8 @@ def HelloReceiver(environ, start_response): response = http.HTTPStatus.OK start_response(f"{response.value} {response.phrase}", []) return ["success".encode("utf-8")] + + # [END gae_mail_handler_receive_wsgi] @@ -54,6 +56,8 @@ def BounceReceiver(environ, start_response): response = http.HTTPStatus.OK start_response(f"{response.value} {response.phrase}", []) return ["success".encode("utf-8")] + + # [END gae_mail_handler_bounce_wsgi] @@ -90,12 +94,17 @@ def HomePage(environ, start_response): print("Error: missing email address") return "Error: Missing email address", 400 - mail.send_mail( - sender=f"demo-app@{project_id}.appspotmail.com", - to=address, - subject="App Engine Outgoing Email", - body=form.get("body")[0], - ) + try: + mail.send_mail( + sender=f"demo-app@{project_id}.appspotmail.com", + to=address, + subject="App Engine Outgoing Email", + body=form.get("body")[0], + ) + except Exception as e: + print(f"Sending mail to {address} failed with exception {e}.") + start_response("500 SERVER ERROR") + return [f"Exception {e} when sending mail to {address}.".encode("utf-8")] print(f"Successfully sent mail to {address}.") diff --git a/appengine/standard_python3/bundled-services/mail/wsgi/main_test.py b/appengine/standard_python3/bundled-services/mail/wsgi/main_test.py index d0026eebf7c0..12eb5c211727 100644 --- a/appengine/standard_python3/bundled-services/mail/wsgi/main_test.py +++ b/appengine/standard_python3/bundled-services/mail/wsgi/main_test.py @@ -90,7 +90,7 @@ def test_send_receive(version): assert "Successfully sent mail" in response.text # Give the mail some time to be delivered and logs to post - time.sleep(30) + time.sleep(60) # Fetch logs to check messages on received mail entries = gcloud_cli(