forked from googlearchive/cloud-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.py
More file actions
65 lines (54 loc) · 1.96 KB
/
Copy patherror.py
File metadata and controls
65 lines (54 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Exceptions raised in playground app."""
import cgi
import httplib
import logging
import sys
import traceback
from . import settings
from . import jsonutil
class PlaygroundError(Exception):
"""Playground specific error class."""
def __init__(self, status_code, message):
super(PlaygroundError, self).__init__(message)
self.status_code = status_code
def __repr__(self):
text = '{}<{} {}>'.format(self.__class__, self.status_code, self.message)
return text.encode('unicode-escape')
def Abort(status_code, message):
logging.debug('Abort {} {}'.format(status_code, message))
raise PlaygroundError(int(status_code), cgi.escape(message))
def MakeErrorResponse(exception, debug_mode):
"""Generate a HTTP error response.
Args:
exception: the underlying cause of the to be created error response.
debug_mode: whether or not a stack trace should be included in the response.
Returns:
HTTP response tuple of consisting of status line, extra headers and
response body.
"""
headers = [
('content-type', 'text/plain; charset=utf-8'),
# Note App Engine automatically sets a 'Date' header for us. See
# https://developers.google.com/appengine/docs/python/runtime#Responses
('Expires', settings.LONG_AGO),
('Cache-Control', 'private, max-age=0'),
]
if isinstance(exception, PlaygroundError):
status_code = exception.status_code
headers.append(
('X-Cloud-Playground-Error', 'True')
)
message = exception.message
else:
logging.exception(exception)
status_code = 500
exc_info = sys.exc_info()
formatted_exception = traceback.format_exception(exc_info[0], exc_info[1],
exc_info[2])
if debug_mode:
message = ('\n{}'.format('\n'.join(formatted_exception)))
else:
message = 'Ouch. How awkward.'
status = '{} {}'.format(status_code, httplib.responses[status_code])
body = message
return status, headers, body