diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py index fa58c2b..09d1210 100644 --- a/IPython/html/base/handlers.py +++ b/IPython/html/base/handlers.py @@ -370,7 +370,8 @@ def json_errors(method): message = e.log_message self.log.warn(message) self.set_status(e.status_code) - self.finish(json.dumps(dict(message=message))) + reply = dict(message=message, reason=e.reason) + self.finish(json.dumps(reply)) except Exception: self.log.error("Unhandled error in API request", exc_info=True) status = 500 @@ -378,7 +379,7 @@ def json_errors(method): t, value, tb = sys.exc_info() self.set_status(status) tb_text = ''.join(traceback.format_exception(t, value, tb)) - reply = dict(message=message, traceback=tb_text) + reply = dict(message=message, reason=None, traceback=tb_text) self.finish(json.dumps(reply)) else: return result diff --git a/IPython/html/services/contents/filemanager.py b/IPython/html/services/contents/filemanager.py index 2ca1f41..6b0bb17 100644 --- a/IPython/html/services/contents/filemanager.py +++ b/IPython/html/services/contents/filemanager.py @@ -282,7 +282,7 @@ class FileContentsManager(ContentsManager): model['content'] = bcontent.decode('utf8') except UnicodeError as e: if format == 'text': - raise web.HTTPError(400, "%s is not UTF-8 encoded" % path) + raise web.HTTPError(400, "%s is not UTF-8 encoded" % path, reason='bad format') else: model['format'] = 'text' @@ -345,14 +345,14 @@ class FileContentsManager(ContentsManager): if os.path.isdir(os_path): if type_ not in (None, 'directory'): raise web.HTTPError(400, - u'%s is a directory, not a %s' % (path, type_)) + u'%s is a directory, not a %s' % (path, type_), reason='bad type') model = self._dir_model(path, content=content) elif type_ == 'notebook' or (type_ is None and path.endswith('.ipynb')): model = self._notebook_model(path, content=content) else: if type_ == 'directory': raise web.HTTPError(400, - u'%s is not a directory') + u'%s is not a directory', reason='bad type') model = self._file_model(path, content=content, format=format) return model diff --git a/IPython/html/static/edit/js/editor.js b/IPython/html/static/edit/js/editor.js index 73b8bb7..ffbdb3a 100644 --- a/IPython/html/static/edit/js/editor.js +++ b/IPython/html/static/edit/js/editor.js @@ -79,6 +79,14 @@ function($, that.events.trigger("file_loaded.Editor", model); }, function(error) { + that.events.trigger("file_load_failed.Editor", error); + if (error.xhr.responseJSON.reason === 'bad format') { + window.location = utils.url_path_join( + that.base_url, + 'files', + that.file_path + ); + } cm.setValue("Error! " + error.message + "\nSaving disabled."); that.save_enabled = false; diff --git a/IPython/html/static/edit/js/main.js b/IPython/html/static/edit/js/main.js index dc92426..2ff2845 100644 --- a/IPython/html/static/edit/js/main.js +++ b/IPython/html/static/edit/js/main.js @@ -73,7 +73,7 @@ require([ page.show(); window.onbeforeunload = function () { - if (!editor.codemirror.isClean(editor.generation)) { + if (editor.save_enabled && !editor.codemirror.isClean(editor.generation)) { return "Unsaved changes will be lost. Close anyway?"; } };