"""Base API handlers""" # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import json try: # py3 from http.client import responses except ImportError: from httplib import responses from tornado import web from ..handlers import BaseHandler class APIHandler(BaseHandler): def get_json_body(self): """Return the body of the request as JSON data.""" if not self.request.body: return None body = self.request.body.strip().decode(u'utf-8') try: model = json.loads(body) except Exception: self.log.debug("Bad JSON: %r", body) self.log.error("Couldn't parse JSON", exc_info=True) raise web.HTTPError(400, 'Invalid JSON in body of request') return model def write_error(self, status_code, **kwargs): """Write JSON errors instead of HTML""" exc_info = kwargs.get('exc_info') message = '' status_message = responses.get(status_code, 'Unknown Error') if exc_info: exception = exc_info[1] # get the custom message, if defined try: message = exception.log_message % exception.args except Exception: pass # construct the custom reason, if defined reason = getattr(exception, 'reason', '') if reason: status_message = reason self.write(json.dumps({ 'status': status_code, 'message': message or status_message, }))