Adding messages to HTTPError raising.

These messages are not returned to the browser, but are printed
to the server logs and will help the code readability.
I have also used a more specific code in some cases.
This commit is contained in:
Brian E. Granger 2011-08-24 11:29:16 -07:00
parent acc81dfbee
commit f1e4a59777
3 changed files with 30 additions and 29 deletions

View File

@ -53,7 +53,7 @@ class NamedNotebookHandler(web.RequestHandler):
def get(self, notebook_id): def get(self, notebook_id):
nbm = self.application.notebook_manager nbm = self.application.notebook_manager
if not nbm.notebook_exists(notebook_id): if not nbm.notebook_exists(notebook_id):
raise web.HTTPError(404) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
self.render('notebook.html', notebook_id=notebook_id) self.render('notebook.html', notebook_id=notebook_id)
@ -311,7 +311,7 @@ class RSTHandler(web.RequestHandler):
def post(self): def post(self):
if publish_string is None: if publish_string is None:
raise web.HTTPError(503) raise web.HTTPError(503, u'docutils not available')
body = self.request.body.strip() body = self.request.body.strip()
source = body source = body
# template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html') # template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html')
@ -326,7 +326,7 @@ class RSTHandler(web.RequestHandler):
settings_overrides=defaults settings_overrides=defaults
) )
except: except:
raise web.HTTPError(400) raise web.HTTPError(400, u'Invalid RST')
print html print html
self.set_header('Content-Type', 'text/html') self.set_header('Content-Type', 'text/html')
self.finish(html) self.finish(html)

View File

@ -278,24 +278,20 @@ class MappingKernelManager(KernelManager):
def kill_kernel(self, kernel_id): def kill_kernel(self, kernel_id):
"""Kill a kernel and remove its notebook association.""" """Kill a kernel and remove its notebook association."""
if kernel_id not in self: self._check_kernel_id(kernel_id)
raise web.HTTPError(404)
super(MappingKernelManager, self).kill_kernel(kernel_id) super(MappingKernelManager, self).kill_kernel(kernel_id)
self.delete_mapping_for_kernel(kernel_id) self.delete_mapping_for_kernel(kernel_id)
self.log.info("Kernel killed: %s" % kernel_id) self.log.info("Kernel killed: %s" % kernel_id)
def interrupt_kernel(self, kernel_id): def interrupt_kernel(self, kernel_id):
"""Interrupt a kernel.""" """Interrupt a kernel."""
if kernel_id not in self: self._check_kernel_id(kernel_id)
raise web.HTTPError(404)
super(MappingKernelManager, self).interrupt_kernel(kernel_id) super(MappingKernelManager, self).interrupt_kernel(kernel_id)
self.log.info("Kernel interrupted: %s" % kernel_id) self.log.info("Kernel interrupted: %s" % kernel_id)
def restart_kernel(self, kernel_id): def restart_kernel(self, kernel_id):
"""Restart a kernel while keeping clients connected.""" """Restart a kernel while keeping clients connected."""
if kernel_id not in self: self._check_kernel_id(kernel_id)
raise web.HTTPError(404)
# Get the notebook_id to preserve the kernel/notebook association. # Get the notebook_id to preserve the kernel/notebook association.
notebook_id = self.notebook_for_kernel(kernel_id) notebook_id = self.notebook_for_kernel(kernel_id)
# Create the new kernel first so we can move the clients over. # Create the new kernel first so we can move the clients over.
@ -309,17 +305,22 @@ class MappingKernelManager(KernelManager):
return new_kernel_id return new_kernel_id
def create_iopub_stream(self, kernel_id): def create_iopub_stream(self, kernel_id):
if kernel_id not in self: """Create a new iopub stream."""
raise web.HTTPError(404) self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_iopub_stream(kernel_id) return super(MappingKernelManager, self).create_iopub_stream(kernel_id)
def create_shell_stream(self, kernel_id): def create_shell_stream(self, kernel_id):
if kernel_id not in self: """Create a new shell stream."""
raise web.HTTPError(404) self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_shell_stream(kernel_id) return super(MappingKernelManager, self).create_shell_stream(kernel_id)
def create_hb_stream(self, kernel_id): def create_hb_stream(self, kernel_id):
if kernel_id not in self: """Create a new hb stream."""
raise web.HTTPError(404) self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_hb_stream(kernel_id) return super(MappingKernelManager, self).create_hb_stream(kernel_id)
def _check_kernel_id(self, kernel_id):
"""Check a that a kernel_id exists and raise 404 if not."""
if kernel_id not in self:
raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id)

View File

@ -93,7 +93,7 @@ class NotebookManager(LoggingConfigurable):
try: try:
name = self.mapping[notebook_id] name = self.mapping[notebook_id]
except KeyError: except KeyError:
raise web.HTTPError(404) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
return self.get_path_by_name(name) return self.get_path_by_name(name)
def get_path_by_name(self, name): def get_path_by_name(self, name):
@ -106,7 +106,7 @@ class NotebookManager(LoggingConfigurable):
"""Get the representation of a notebook in format by notebook_id.""" """Get the representation of a notebook in format by notebook_id."""
format = unicode(format) format = unicode(format)
if format not in self.allowed_formats: if format not in self.allowed_formats:
raise web.HTTPError(415) raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
last_modified, nb = self.get_notebook_object(notebook_id) last_modified, nb = self.get_notebook_object(notebook_id)
data = current.writes(nb, format) data = current.writes(nb, format)
name = nb.get('name','notebook') name = nb.get('name','notebook')
@ -116,7 +116,7 @@ class NotebookManager(LoggingConfigurable):
"""Get the NotebookNode representation of a notebook by notebook_id.""" """Get the NotebookNode representation of a notebook by notebook_id."""
path = self.find_path(notebook_id) path = self.find_path(notebook_id)
if not os.path.isfile(path): if not os.path.isfile(path):
raise web.HTTPError(404) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
info = os.stat(path) info = os.stat(path)
last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime) last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime)
with open(path,'r') as f: with open(path,'r') as f:
@ -125,7 +125,7 @@ class NotebookManager(LoggingConfigurable):
# v1 and v2 and json in the .ipynb files. # v1 and v2 and json in the .ipynb files.
nb = current.reads(s, u'json') nb = current.reads(s, u'json')
except: except:
raise web.HTTPError(404) raise web.HTTPError(500, u'Unreadable JSON notebook.')
if 'name' not in nb: if 'name' not in nb:
nb.name = os.path.split(path)[-1].split(u'.')[0] nb.name = os.path.split(path)[-1].split(u'.')[0]
return last_modified, nb return last_modified, nb
@ -137,18 +137,18 @@ class NotebookManager(LoggingConfigurable):
and the value in the data is updated to use that value. and the value in the data is updated to use that value.
""" """
if format not in self.allowed_formats: if format not in self.allowed_formats:
raise web.HTTPError(415) raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
try: try:
nb = current.reads(data, format) nb = current.reads(data, format)
except: except:
raise web.HTTPError(400) raise web.HTTPError(400, u'Invalid JSON data')
if name is None: if name is None:
try: try:
name = nb.metadata.name name = nb.metadata.name
except AttributeError: except AttributeError:
raise web.HTTPError(400) raise web.HTTPError(400, u'Missing notebook name')
nb.metadata.name = name nb.metadata.name = name
notebook_id = self.new_notebook_id(name) notebook_id = self.new_notebook_id(name)
@ -158,12 +158,12 @@ class NotebookManager(LoggingConfigurable):
def save_notebook(self, notebook_id, data, name=None, format=u'json'): def save_notebook(self, notebook_id, data, name=None, format=u'json'):
"""Save an existing notebook by notebook_id.""" """Save an existing notebook by notebook_id."""
if format not in self.allowed_formats: if format not in self.allowed_formats:
raise web.HTTPError(415) raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
try: try:
nb = current.reads(data, format) nb = current.reads(data, format)
except: except:
raise web.HTTPError(400) raise web.HTTPError(400, u'Invalid JSON data')
if name is not None: if name is not None:
nb.metadata.name = name nb.metadata.name = name
@ -172,18 +172,18 @@ class NotebookManager(LoggingConfigurable):
def save_notebook_object(self, notebook_id, nb): def save_notebook_object(self, notebook_id, nb):
"""Save an existing notebook object by notebook_id.""" """Save an existing notebook object by notebook_id."""
if notebook_id not in self.mapping: if notebook_id not in self.mapping:
raise web.HTTPError(404) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
old_name = self.mapping[notebook_id] old_name = self.mapping[notebook_id]
try: try:
new_name = nb.metadata.name new_name = nb.metadata.name
except AttributeError: except AttributeError:
raise web.HTTPError(400) raise web.HTTPError(400, u'Missing notebook name')
path = self.get_path_by_name(new_name) path = self.get_path_by_name(new_name)
try: try:
with open(path,'w') as f: with open(path,'w') as f:
current.write(nb, f, u'json') current.write(nb, f, u'json')
except: except:
raise web.HTTPError(400) raise web.HTTPError(400, u'Unexpected error while saving notebook')
if old_name != new_name: if old_name != new_name:
old_path = self.get_path_by_name(old_name) old_path = self.get_path_by_name(old_name)
if os.path.isfile(old_path): if os.path.isfile(old_path):
@ -195,7 +195,7 @@ class NotebookManager(LoggingConfigurable):
"""Delete notebook by notebook_id.""" """Delete notebook by notebook_id."""
path = self.find_path(notebook_id) path = self.find_path(notebook_id)
if not os.path.isfile(path): if not os.path.isfile(path):
raise web.HTTPError(404) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
os.unlink(path) os.unlink(path)
self.delete_notebook_id(notebook_id) self.delete_notebook_id(notebook_id)