##// END OF EJS Templates
Handle NoSuchKernel errors more gracefully
Jessica B. Hamrick -
Show More
@@ -1,113 +1,118 b''
1 1 """Tornado handlers for the sessions web service."""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import json
7 7
8 8 from tornado import web
9 9
10 10 from ...base.handlers import IPythonHandler, json_errors
11 11 from IPython.utils.jsonutil import date_default
12 12 from IPython.html.utils import url_path_join, url_escape
13 from IPython.kernel.kernelspec import NoSuchKernel
13 14
14 15
15 16 class SessionRootHandler(IPythonHandler):
16 17
17 18 @web.authenticated
18 19 @json_errors
19 20 def get(self):
20 21 # Return a list of running sessions
21 22 sm = self.session_manager
22 23 sessions = sm.list_sessions()
23 24 self.finish(json.dumps(sessions, default=date_default))
24 25
25 26 @web.authenticated
26 27 @json_errors
27 28 def post(self):
28 29 # Creates a new session
29 30 #(unless a session already exists for the named nb)
30 31 sm = self.session_manager
31 32 cm = self.contents_manager
32 33 km = self.kernel_manager
33 34
34 35 model = self.get_json_body()
35 36 if model is None:
36 37 raise web.HTTPError(400, "No JSON data provided")
37 38 try:
38 39 name = model['notebook']['name']
39 40 except KeyError:
40 41 raise web.HTTPError(400, "Missing field in JSON data: notebook.name")
41 42 try:
42 43 path = model['notebook']['path']
43 44 except KeyError:
44 45 raise web.HTTPError(400, "Missing field in JSON data: notebook.path")
45 46 try:
46 47 kernel_name = model['kernel']['name']
47 48 except KeyError:
48 49 self.log.debug("No kernel name specified, using default kernel")
49 50 kernel_name = None
50 51
51 52 # Check to see if session exists
52 53 if sm.session_exists(name=name, path=path):
53 54 model = sm.get_session(name=name, path=path)
54 55 else:
55 model = sm.create_session(name=name, path=path, kernel_name=kernel_name)
56 try:
57 model = sm.create_session(name=name, path=path, kernel_name=kernel_name)
58 except NoSuchKernel:
59 raise web.HTTPError(400, "No such kernel: %s" % kernel_name)
60
56 61 location = url_path_join(self.base_url, 'api', 'sessions', model['id'])
57 62 self.set_header('Location', url_escape(location))
58 63 self.set_status(201)
59 64 self.finish(json.dumps(model, default=date_default))
60 65
61 66 class SessionHandler(IPythonHandler):
62 67
63 68 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
64 69
65 70 @web.authenticated
66 71 @json_errors
67 72 def get(self, session_id):
68 73 # Returns the JSON model for a single session
69 74 sm = self.session_manager
70 75 model = sm.get_session(session_id=session_id)
71 76 self.finish(json.dumps(model, default=date_default))
72 77
73 78 @web.authenticated
74 79 @json_errors
75 80 def patch(self, session_id):
76 81 # Currently, this handler is strictly for renaming notebooks
77 82 sm = self.session_manager
78 83 model = self.get_json_body()
79 84 if model is None:
80 85 raise web.HTTPError(400, "No JSON data provided")
81 86 changes = {}
82 87 if 'notebook' in model:
83 88 notebook = model['notebook']
84 89 if 'name' in notebook:
85 90 changes['name'] = notebook['name']
86 91 if 'path' in notebook:
87 92 changes['path'] = notebook['path']
88 93
89 94 sm.update_session(session_id, **changes)
90 95 model = sm.get_session(session_id=session_id)
91 96 self.finish(json.dumps(model, default=date_default))
92 97
93 98 @web.authenticated
94 99 @json_errors
95 100 def delete(self, session_id):
96 101 # Deletes the session with given session_id
97 102 sm = self.session_manager
98 103 sm.delete_session(session_id)
99 104 self.set_status(204)
100 105 self.finish()
101 106
102 107
103 108 #-----------------------------------------------------------------------------
104 109 # URL to handler mappings
105 110 #-----------------------------------------------------------------------------
106 111
107 112 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
108 113
109 114 default_handlers = [
110 115 (r"/api/sessions/%s" % _session_id_regex, SessionHandler),
111 116 (r"/api/sessions", SessionRootHandler)
112 117 ]
113 118
General Comments 0
You need to be logged in to leave comments. Login now