Skip to content

Commit b68ae9c

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #286 from GoogleCloudPlatform/managedvms-errors
Adding error handlers to all managed vms samples.
2 parents 80e9e73 + 4d6283c commit b68ae9c

File tree

14 files changed

+142
-1
lines changed

14 files changed

+142
-1
lines changed

managed_vms/analytics/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import os
1718

1819
from flask import Flask
@@ -57,6 +58,15 @@ def track_example():
5758
return 'Event tracked.'
5859

5960

61+
@app.errorhandler(500)
62+
def server_error(e):
63+
logging.exception('An error ocurred during a request.')
64+
return """
65+
An internal error occurred: <pre>{}</pre>
66+
See logs for full stacktrace.
67+
""".format(e), 500
68+
69+
6070
if __name__ == '__main__':
6171
# This is used when running locally. Gunicorn is used to run the
6272
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/cloudsql/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import datetime
16+
import logging
1617
import os
1718
import socket
1819

@@ -80,6 +81,15 @@ def index():
8081
# [END example]
8182

8283

84+
@app.errorhandler(500)
85+
def server_error(e):
86+
logging.exception('An error ocurred during a request.')
87+
return """
88+
An internal error occurred: <pre>{}</pre>
89+
See logs for full stacktrace.
90+
""".format(e), 500
91+
92+
8393
if __name__ == '__main__':
8494
# This is used when running locally. Gunicorn is used to run the
8595
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/datastore/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import datetime
16+
import logging
1617
import os
1718
import socket
1819

@@ -65,6 +66,15 @@ def index():
6566
# [END example]
6667

6768

69+
@app.errorhandler(500)
70+
def server_error(e):
71+
logging.exception('An error ocurred during a request.')
72+
return """
73+
An internal error occurred: <pre>{}</pre>
74+
See logs for full stacktrace.
75+
""".format(e), 500
76+
77+
6878
if __name__ == '__main__':
6979
# This is used when running locally. Gunicorn is used to run the
7080
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/disk/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
import os
1617
import socket
1718

@@ -58,6 +59,15 @@ def index():
5859
# [END example]
5960

6061

62+
@app.errorhandler(500)
63+
def server_error(e):
64+
logging.exception('An error ocurred during a request.')
65+
return """
66+
An internal error occurred: <pre>{}</pre>
67+
See logs for full stacktrace.
68+
""".format(e), 500
69+
70+
6171
if __name__ == '__main__':
6272
# This is used when running locally. Gunicorn is used to run the
6373
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/extending_runtime/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import subprocess
1718

1819
from flask import Flask
@@ -29,6 +30,15 @@ def fortune():
2930
# [END example]
3031

3132

33+
@app.errorhandler(500)
34+
def server_error(e):
35+
logging.exception('An error ocurred during a request.')
36+
return """
37+
An internal error occurred: <pre>{}</pre>
38+
See logs for full stacktrace.
39+
""".format(e), 500
40+
41+
3242
if __name__ == '__main__':
3343
# This is used when running locally. Gunicorn is used to run the
3444
# application on Google App Engine. See CMD in Dockerfile.

managed_vms/extending_runtime_compat/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import subprocess
1718

1819
from flask import Flask
@@ -27,4 +28,14 @@ def fortune():
2728
output = subprocess.check_output('/usr/games/fortune')
2829
return output, 200, {'Content-Type': 'text/plain; charset=utf-8'}
2930
# [END example]
31+
32+
33+
@app.errorhandler(500)
34+
def server_error(e):
35+
logging.exception('An error ocurred during a request.')
36+
return """
37+
An internal error occurred: <pre>{}</pre>
38+
See logs for full stacktrace.
39+
""".format(e), 500
40+
3041
# [END app]

managed_vms/mailgun/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import os
1718

1819
from flask import Flask, render_template, request
@@ -78,6 +79,15 @@ def send_email():
7879
return 'Email sent.'
7980

8081

82+
@app.errorhandler(500)
83+
def server_error(e):
84+
logging.exception('An error ocurred during a request.')
85+
return """
86+
An internal error occurred: <pre>{}</pre>
87+
See logs for full stacktrace.
88+
""".format(e), 500
89+
90+
8191
if __name__ == '__main__':
8292
# This is used when running locally. Gunicorn is used to run the
8393
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/memcache/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
import os
1617

1718
from flask import Flask
@@ -42,6 +43,15 @@ def index():
4243
# [END example]
4344

4445

46+
@app.errorhandler(500)
47+
def server_error(e):
48+
logging.exception('An error ocurred during a request.')
49+
return """
50+
An internal error occurred: <pre>{}</pre>
51+
See logs for full stacktrace.
52+
""".format(e), 500
53+
54+
4555
if __name__ == '__main__':
4656
# This is used when running locally. Gunicorn is used to run the
4757
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/pubsub/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# [START app]
1616
import base64
1717
import json
18+
import logging
1819
import os
1920

2021
from flask import current_app, Flask, render_template, request
@@ -68,6 +69,15 @@ def pubsub_push():
6869
# [END push]
6970

7071

72+
@app.errorhandler(500)
73+
def server_error(e):
74+
logging.exception('An error ocurred during a request.')
75+
return """
76+
An internal error occurred: <pre>{}</pre>
77+
See logs for full stacktrace.
78+
""".format(e), 500
79+
80+
7181
if __name__ == '__main__':
7282
# This is used when running locally. Gunicorn is used to run the
7383
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/sendgrid/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import os
1718

1819
from flask import Flask, render_template, request
@@ -57,6 +58,15 @@ def send_email():
5758
# [END example]
5859

5960

61+
@app.errorhandler(500)
62+
def server_error(e):
63+
logging.exception('An error ocurred during a request.')
64+
return """
65+
An internal error occurred: <pre>{}</pre>
66+
See logs for full stacktrace.
67+
""".format(e), 500
68+
69+
6070
if __name__ == '__main__':
6171
# This is used when running locally. Gunicorn is used to run the
6272
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/static_files/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
17+
1618
from flask import Flask, render_template
1719

1820

@@ -24,6 +26,15 @@ def hello():
2426
return render_template('index.html')
2527

2628

29+
@app.errorhandler(500)
30+
def server_error(e):
31+
logging.exception('An error ocurred during a request.')
32+
return """
33+
An internal error occurred: <pre>{}</pre>
34+
See logs for full stacktrace.
35+
""".format(e), 500
36+
37+
2738
if __name__ == '__main__':
2839
# This is used when running locally. Gunicorn is used to run the
2940
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/storage/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import os
1718

1819
from flask import Flask, request
@@ -30,7 +31,6 @@
3031
# [START form]
3132
@app.route('/')
3233
def index():
33-
"""Present the user with an upload form."""
3434
return """
3535
<form method="POST" action="/upload" enctype="multipart/form-data">
3636
<input type="file" name="file">
@@ -68,6 +68,15 @@ def upload():
6868
# [END upload]
6969

7070

71+
@app.errorhandler(500)
72+
def server_error(e):
73+
logging.exception('An error ocurred during a request.')
74+
return """
75+
An internal error occurred: <pre>{}</pre>
76+
See logs for full stacktrace.
77+
""".format(e), 500
78+
79+
7180
if __name__ == '__main__':
7281
# This is used when running locally. Gunicorn is used to run the
7382
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/twilio/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
# [START app]
16+
import logging
1617
import os
1718

1819
from flask import Flask, request
@@ -73,6 +74,15 @@ def receive_sms():
7374
# [END receive_sms]
7475

7576

77+
@app.errorhandler(500)
78+
def server_error(e):
79+
logging.exception('An error ocurred during a request.')
80+
return """
81+
An internal error occurred: <pre>{}</pre>
82+
See logs for full stacktrace.
83+
""".format(e), 500
84+
85+
7686
if __name__ == '__main__':
7787
# This is used when running locally. Gunicorn is used to run the
7888
# application on Google App Engine. See entrypoint in app.yaml.

managed_vms/websockets/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ def index():
6363
return render_template('index.html', external_ip=external_ip)
6464
# [END app]
6565

66+
67+
@app.errorhandler(500)
68+
def server_error(e):
69+
logging.exception('An error ocurred during a request.')
70+
return """
71+
An internal error occurred: <pre>{}</pre>
72+
See logs for full stacktrace.
73+
""".format(e), 500
74+
75+
6676
if __name__ == '__main__':
6777
print("""
6878
This can not be run directly because the Flask development server does not

0 commit comments

Comments
 (0)