mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-16 14:33:00 +00:00
try harder to make a useful error message when API requests are made to a not-running server
include link to spawn page
This commit is contained in:
@@ -1159,20 +1159,28 @@ class UserUrlHandler(BaseHandler):
|
|||||||
Note that this only occurs if bob's server is not already running.
|
Note that this only occurs if bob's server is not already running.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _fail_api_request(self, *args, **kwargs):
|
def _fail_api_request(self, user_name='', server_name=''):
|
||||||
"""Fail an API request to a not-running server"""
|
"""Fail an API request to a not-running server"""
|
||||||
self.log.warning(
|
self.log.warning(
|
||||||
"Failing suspected API request to not-running server: %s", self.request.uri
|
"Failing suspected API request to not-running server: %s", self.request.path
|
||||||
)
|
)
|
||||||
self.set_status(503)
|
self.set_status(503)
|
||||||
self.set_header("Content-Type", "application/json")
|
self.set_header("Content-Type", "application/json")
|
||||||
|
|
||||||
|
spawn_url = urlparse(self.request.full_url())._replace(query="")
|
||||||
|
spawn_path_parts = [self.hub.base_url, "spawn", user_name]
|
||||||
|
if server_name:
|
||||||
|
spawn_path_parts.append(server_name)
|
||||||
|
spawn_url = urlunparse(
|
||||||
|
spawn_url._replace(path=url_path_join(*spawn_path_parts))
|
||||||
|
)
|
||||||
self.write(
|
self.write(
|
||||||
json.dumps(
|
json.dumps(
|
||||||
{
|
{
|
||||||
"message": (
|
"message": (
|
||||||
"No jupyterhub server running at {}."
|
"JupyterHub server no longer running at {}."
|
||||||
" Restart from the Hub home page {}"
|
" Restart the server at {}"
|
||||||
).format(self.request.uri, self.hub.base_url)
|
).format(self.request.path[len(self.hub.base_url) - 1 :], spawn_url)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -1180,9 +1188,32 @@ class UserUrlHandler(BaseHandler):
|
|||||||
|
|
||||||
# fail all non-GET requests with JSON
|
# fail all non-GET requests with JSON
|
||||||
# assuming they are API requests
|
# assuming they are API requests
|
||||||
post = _fail_api_request
|
|
||||||
patch = _fail_api_request
|
def non_get(self, user_name, user_path):
|
||||||
delete = _fail_api_request
|
"""Handle non-get requests
|
||||||
|
|
||||||
|
These all fail with a hopefully informative message
|
||||||
|
pointing to how to spawn a stopped server
|
||||||
|
"""
|
||||||
|
if (
|
||||||
|
user_name
|
||||||
|
and user_path
|
||||||
|
and self.allow_named_servers
|
||||||
|
and self.current_user
|
||||||
|
and user_name == self.current_user.name
|
||||||
|
):
|
||||||
|
server_name = user_path.split('/', 1)[0]
|
||||||
|
if server_name not in self.current_user.orm_user.orm_spawners:
|
||||||
|
# no such server, assume default
|
||||||
|
server_name = ''
|
||||||
|
else:
|
||||||
|
server_name = ''
|
||||||
|
|
||||||
|
self._fail_api_request(user_name, server_name)
|
||||||
|
|
||||||
|
post = non_get
|
||||||
|
patch = non_get
|
||||||
|
delete = non_get
|
||||||
|
|
||||||
@web.authenticated
|
@web.authenticated
|
||||||
async def get(self, user_name, user_path):
|
async def get(self, user_name, user_path):
|
||||||
@@ -1280,7 +1311,7 @@ class UserUrlHandler(BaseHandler):
|
|||||||
self.request.headers.get('Accept', ''),
|
self.request.headers.get('Accept', ''),
|
||||||
choices=['application/json', 'text/html'],
|
choices=['application/json', 'text/html'],
|
||||||
) == 'application/json' or 'api' in user_path.split('/'):
|
) == 'application/json' or 'api' in user_path.split('/'):
|
||||||
self._fail_api_request()
|
self._fail_api_request(user_name, server_name)
|
||||||
return
|
return
|
||||||
|
|
||||||
pending_url = url_concat(
|
pending_url = url_concat(
|
||||||
|
@@ -411,7 +411,7 @@ async def test_login_redirect(app, running, next_url, location):
|
|||||||
if location:
|
if location:
|
||||||
location = ujoin(app.base_url, location)
|
location = ujoin(app.base_url, location)
|
||||||
elif running:
|
elif running:
|
||||||
location = ujoin(app.base_url, 'user/river/')
|
location = public_url(app, user)
|
||||||
else:
|
else:
|
||||||
# use default url
|
# use default url
|
||||||
location = ujoin(app.base_url, 'hub/spawn')
|
location = ujoin(app.base_url, 'hub/spawn')
|
||||||
@@ -697,8 +697,8 @@ async def test_server_not_running_api_request(app):
|
|||||||
assert r.status_code == 503
|
assert r.status_code == 503
|
||||||
assert r.headers["content-type"] == "application/json"
|
assert r.headers["content-type"] == "application/json"
|
||||||
message = r.json()['message']
|
message = r.json()['message']
|
||||||
assert "Hub home page" in message
|
assert ujoin(app.base_url, "hub/spawn/bees") in message
|
||||||
assert "/user/bees" in message
|
assert " /user/bees" in message
|
||||||
|
|
||||||
|
|
||||||
async def test_metrics_no_auth(app):
|
async def test_metrics_no_auth(app):
|
||||||
|
Reference in New Issue
Block a user