diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index 7aa3c81..1530aae 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -53,7 +53,7 @@ class NamedNotebookHandler(web.RequestHandler): def get(self, notebook_id): nbm = self.application.notebook_manager 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) @@ -311,7 +311,7 @@ class RSTHandler(web.RequestHandler): def post(self): if publish_string is None: - raise web.HTTPError(503) + raise web.HTTPError(503, u'docutils not available') body = self.request.body.strip() source = body # 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 ) except: - raise web.HTTPError(400) + raise web.HTTPError(400, u'Invalid RST') print html self.set_header('Content-Type', 'text/html') self.finish(html) diff --git a/IPython/frontend/html/notebook/kernelmanager.py b/IPython/frontend/html/notebook/kernelmanager.py index 355d147..96e322e 100644 --- a/IPython/frontend/html/notebook/kernelmanager.py +++ b/IPython/frontend/html/notebook/kernelmanager.py @@ -278,24 +278,20 @@ class MappingKernelManager(KernelManager): def kill_kernel(self, kernel_id): """Kill a kernel and remove its notebook association.""" - if kernel_id not in self: - raise web.HTTPError(404) + self._check_kernel_id(kernel_id) super(MappingKernelManager, self).kill_kernel(kernel_id) self.delete_mapping_for_kernel(kernel_id) self.log.info("Kernel killed: %s" % kernel_id) def interrupt_kernel(self, kernel_id): """Interrupt a kernel.""" - if kernel_id not in self: - raise web.HTTPError(404) + self._check_kernel_id(kernel_id) super(MappingKernelManager, self).interrupt_kernel(kernel_id) self.log.info("Kernel interrupted: %s" % kernel_id) def restart_kernel(self, kernel_id): """Restart a kernel while keeping clients connected.""" - if kernel_id not in self: - raise web.HTTPError(404) - + self._check_kernel_id(kernel_id) # Get the notebook_id to preserve the kernel/notebook association. notebook_id = self.notebook_for_kernel(kernel_id) # Create the new kernel first so we can move the clients over. @@ -309,17 +305,22 @@ class MappingKernelManager(KernelManager): return new_kernel_id def create_iopub_stream(self, kernel_id): - if kernel_id not in self: - raise web.HTTPError(404) + """Create a new iopub stream.""" + self._check_kernel_id(kernel_id) return super(MappingKernelManager, self).create_iopub_stream(kernel_id) def create_shell_stream(self, kernel_id): - if kernel_id not in self: - raise web.HTTPError(404) + """Create a new shell stream.""" + self._check_kernel_id(kernel_id) return super(MappingKernelManager, self).create_shell_stream(kernel_id) def create_hb_stream(self, kernel_id): - if kernel_id not in self: - raise web.HTTPError(404) + """Create a new hb stream.""" + self._check_kernel_id(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) + diff --git a/IPython/frontend/html/notebook/notebookmanager.py b/IPython/frontend/html/notebook/notebookmanager.py index 7607e86..4ec9517 100644 --- a/IPython/frontend/html/notebook/notebookmanager.py +++ b/IPython/frontend/html/notebook/notebookmanager.py @@ -93,7 +93,7 @@ class NotebookManager(LoggingConfigurable): try: name = self.mapping[notebook_id] 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) 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.""" format = unicode(format) 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) data = current.writes(nb, format) name = nb.get('name','notebook') @@ -116,7 +116,7 @@ class NotebookManager(LoggingConfigurable): """Get the NotebookNode representation of a notebook by notebook_id.""" path = self.find_path(notebook_id) 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) last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime) with open(path,'r') as f: @@ -125,7 +125,7 @@ class NotebookManager(LoggingConfigurable): # v1 and v2 and json in the .ipynb files. nb = current.reads(s, u'json') except: - raise web.HTTPError(404) + raise web.HTTPError(500, u'Unreadable JSON notebook.') if 'name' not in nb: nb.name = os.path.split(path)[-1].split(u'.')[0] return last_modified, nb @@ -137,18 +137,18 @@ class NotebookManager(LoggingConfigurable): and the value in the data is updated to use that value. """ if format not in self.allowed_formats: - raise web.HTTPError(415) + raise web.HTTPError(415, u'Invalid notebook format: %s' % format) try: nb = current.reads(data, format) except: - raise web.HTTPError(400) + raise web.HTTPError(400, u'Invalid JSON data') if name is None: try: name = nb.metadata.name except AttributeError: - raise web.HTTPError(400) + raise web.HTTPError(400, u'Missing notebook name') nb.metadata.name = 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'): """Save an existing notebook by notebook_id.""" if format not in self.allowed_formats: - raise web.HTTPError(415) + raise web.HTTPError(415, u'Invalid notebook format: %s' % format) try: nb = current.reads(data, format) except: - raise web.HTTPError(400) + raise web.HTTPError(400, u'Invalid JSON data') if name is not None: nb.metadata.name = name @@ -172,18 +172,18 @@ class NotebookManager(LoggingConfigurable): def save_notebook_object(self, notebook_id, nb): """Save an existing notebook object by notebook_id.""" 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] try: new_name = nb.metadata.name except AttributeError: - raise web.HTTPError(400) + raise web.HTTPError(400, u'Missing notebook name') path = self.get_path_by_name(new_name) try: with open(path,'w') as f: current.write(nb, f, u'json') except: - raise web.HTTPError(400) + raise web.HTTPError(400, u'Unexpected error while saving notebook') if old_name != new_name: old_path = self.get_path_by_name(old_name) if os.path.isfile(old_path): @@ -195,7 +195,7 @@ class NotebookManager(LoggingConfigurable): """Delete notebook by notebook_id.""" path = self.find_path(notebook_id) 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) self.delete_notebook_id(notebook_id)