From 4d6283ca1dfd33ff1cc465a85dbedd276079534c Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Tue, 26 Apr 2016 12:56:58 -0700 Subject: [PATCH] Adding error handlers to all managed vms samples. Change-Id: I1010699be5bbbf5f8356e2a0a27e0c2f836fdaf9 --- managed_vms/analytics/main.py | 10 ++++++++++ managed_vms/cloudsql/main.py | 10 ++++++++++ managed_vms/datastore/main.py | 10 ++++++++++ managed_vms/disk/main.py | 10 ++++++++++ managed_vms/extending_runtime/main.py | 10 ++++++++++ managed_vms/extending_runtime_compat/main.py | 11 +++++++++++ managed_vms/mailgun/main.py | 10 ++++++++++ managed_vms/memcache/main.py | 10 ++++++++++ managed_vms/pubsub/main.py | 10 ++++++++++ managed_vms/sendgrid/main.py | 10 ++++++++++ managed_vms/static_files/main.py | 11 +++++++++++ managed_vms/storage/main.py | 11 ++++++++++- managed_vms/twilio/main.py | 10 ++++++++++ managed_vms/websockets/main.py | 10 ++++++++++ 14 files changed, 142 insertions(+), 1 deletion(-) diff --git a/managed_vms/analytics/main.py b/managed_vms/analytics/main.py index 49649705762..b12c8cc5488 100644 --- a/managed_vms/analytics/main.py +++ b/managed_vms/analytics/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import os from flask import Flask @@ -57,6 +58,15 @@ def track_example(): return 'Event tracked.' +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/cloudsql/main.py b/managed_vms/cloudsql/main.py index baff9df43f9..f3ba7a41d61 100644 --- a/managed_vms/cloudsql/main.py +++ b/managed_vms/cloudsql/main.py @@ -13,6 +13,7 @@ # limitations under the License. import datetime +import logging import os import socket @@ -80,6 +81,15 @@ def index(): # [END example] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/datastore/main.py b/managed_vms/datastore/main.py index c924ae6d453..9c5b06b5c13 100644 --- a/managed_vms/datastore/main.py +++ b/managed_vms/datastore/main.py @@ -13,6 +13,7 @@ # limitations under the License. import datetime +import logging import os import socket @@ -65,6 +66,15 @@ def index(): # [END example] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/disk/main.py b/managed_vms/disk/main.py index ee5c9ed5018..f4ec27fb611 100644 --- a/managed_vms/disk/main.py +++ b/managed_vms/disk/main.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging import os import socket @@ -58,6 +59,15 @@ def index(): # [END example] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/extending_runtime/main.py b/managed_vms/extending_runtime/main.py index 1c75a7bbff2..d302052ce6b 100644 --- a/managed_vms/extending_runtime/main.py +++ b/managed_vms/extending_runtime/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import subprocess from flask import Flask @@ -29,6 +30,15 @@ def fortune(): # [END example] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See CMD in Dockerfile. diff --git a/managed_vms/extending_runtime_compat/main.py b/managed_vms/extending_runtime_compat/main.py index e9bc3eacfa1..5fd2c3718f5 100644 --- a/managed_vms/extending_runtime_compat/main.py +++ b/managed_vms/extending_runtime_compat/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import subprocess from flask import Flask @@ -27,4 +28,14 @@ def fortune(): output = subprocess.check_output('/usr/games/fortune') return output, 200, {'Content-Type': 'text/plain; charset=utf-8'} # [END example] + + +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + # [END app] diff --git a/managed_vms/mailgun/main.py b/managed_vms/mailgun/main.py index 0ba388bccea..140c24c9304 100644 --- a/managed_vms/mailgun/main.py +++ b/managed_vms/mailgun/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import os from flask import Flask, render_template, request @@ -78,6 +79,15 @@ def send_email(): return 'Email sent.' +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/memcache/main.py b/managed_vms/memcache/main.py index 3cda68026a0..77b790a4f5c 100644 --- a/managed_vms/memcache/main.py +++ b/managed_vms/memcache/main.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging import os from flask import Flask @@ -42,6 +43,15 @@ def index(): # [END example] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/pubsub/main.py b/managed_vms/pubsub/main.py index f9b3893fb4a..f813b823c17 100644 --- a/managed_vms/pubsub/main.py +++ b/managed_vms/pubsub/main.py @@ -15,6 +15,7 @@ # [START app] import base64 import json +import logging import os from flask import current_app, Flask, render_template, request @@ -68,6 +69,15 @@ def pubsub_push(): # [END push] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/sendgrid/main.py b/managed_vms/sendgrid/main.py index 2bee1c10699..43d6b7e38ea 100644 --- a/managed_vms/sendgrid/main.py +++ b/managed_vms/sendgrid/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import os from flask import Flask, render_template, request @@ -57,6 +58,15 @@ def send_email(): # [END example] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/static_files/main.py b/managed_vms/static_files/main.py index 4ae0c71a4da..297809b4b5e 100644 --- a/managed_vms/static_files/main.py +++ b/managed_vms/static_files/main.py @@ -13,6 +13,8 @@ # limitations under the License. # [START app] +import logging + from flask import Flask, render_template @@ -24,6 +26,15 @@ def hello(): return render_template('index.html') +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/storage/main.py b/managed_vms/storage/main.py index 26ebb55f7ed..c3207e60c92 100644 --- a/managed_vms/storage/main.py +++ b/managed_vms/storage/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import os from flask import Flask, request @@ -30,7 +31,6 @@ # [START form] @app.route('/') def index(): - """Present the user with an upload form.""" return """
@@ -68,6 +68,15 @@ def upload(): # [END upload] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/twilio/main.py b/managed_vms/twilio/main.py index 3bbe2caec94..d3279d80a7a 100644 --- a/managed_vms/twilio/main.py +++ b/managed_vms/twilio/main.py @@ -13,6 +13,7 @@ # limitations under the License. # [START app] +import logging import os from flask import Flask, request @@ -73,6 +74,15 @@ def receive_sms(): # [END receive_sms] +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. diff --git a/managed_vms/websockets/main.py b/managed_vms/websockets/main.py index 5e690508840..857e93907ab 100644 --- a/managed_vms/websockets/main.py +++ b/managed_vms/websockets/main.py @@ -63,6 +63,16 @@ def index(): return render_template('index.html', external_ip=external_ip) # [END app] + +@app.errorhandler(500) +def server_error(e): + logging.exception('An error ocurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + if __name__ == '__main__': print(""" This can not be run directly because the Flask development server does not