Skip to content

Commit d1b7d9a

Browse files
authored
Merge pull request #263 from kevin-bates/fix-262-jsonerrorsmixin
Fix #262: JSONErrorsMixin not returning correct results
2 parents fc1d714 + 9896a01 commit d1b7d9a

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

kernel_gateway/mixins.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""Mixins for Tornado handlers."""
44

55
import json
6+
import traceback
7+
from tornado import web
68
try:
79
# py3
810
from http.client import responses
@@ -85,12 +87,8 @@ class JSONErrorsMixin(object):
8587
def write_error(self, status_code, **kwargs):
8688
"""Responds with an application/json error object.
8789
88-
Overrides the HTML renderer in the notebook server to force all errors
89-
to JSON format. Outputs all errors as JSON in the same format as the
90-
notebook.base.handlers.json_errors decorator. Avoids having to
91-
(re-)decorate everything that the kernel gateway overrides. Also avoids
92-
rendering errors as a human readable HTML pages in cases where the
93-
decorator is not used in the notebook code base.
90+
Overrides the APIHandler.write_error in the notebook server until it
91+
properly sets the 'reason' field.
9492
9593
Parameters
9694
----------
@@ -108,20 +106,24 @@ def write_error(self, status_code, **kwargs):
108106
exc_info = kwargs.get('exc_info')
109107
message = ''
110108
reason = responses.get(status_code, 'Unknown HTTP Error')
109+
reply = {
110+
'reason': reason,
111+
'message': message,
112+
}
111113
if exc_info:
112114
exception = exc_info[1]
113115
# Get the custom message, if defined
114-
try:
115-
message = exception.log_message % exception.args
116-
except Exception:
117-
pass
116+
if isinstance(exception, web.HTTPError):
117+
reply['message'] = exception.log_message or message
118+
else:
119+
reply['message'] = 'Unknown server error'
120+
reply['traceback'] = ''.join(traceback.format_exception(*exc_info))
118121

119122
# Construct the custom reason, if defined
120123
custom_reason = getattr(exception, 'reason', '')
121124
if custom_reason:
122-
reason = custom_reason
125+
reply['reason'] = custom_reason
123126

124127
self.set_header('Content-Type', 'application/json')
125128
self.set_status(status_code)
126-
reply = dict(reason=reason, message=message)
127129
self.finish(json.dumps(reply))

0 commit comments

Comments
 (0)