Skip to content

Commit 04553ec

Browse files
committed
squashme: dont use error classes from notebooks
We have shared errors we raise everywhere which we should use not that we moved the notebooks code. And we should avoid using the notebooks errors that came with the old code. We will fully clean out these old error classes in some follow up work.
1 parent f90d362 commit 04553ec

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

components/renku_data_services/notebooks/blueprints.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from gitlab.v4.objects.projects import Project as GitlabProject
1717
from kubernetes.client import V1ObjectMeta, V1Secret
1818
from marshmallow import ValidationError
19-
from sanic import Request, empty, exceptions, json
19+
from sanic import Request, empty, json
2020
from sanic.log import logger
2121
from sanic.response import HTTPResponse, JSONResponse
2222
from sanic_ext import validate
@@ -75,7 +75,7 @@
7575
)
7676
from renku_data_services.notebooks.errors.intermittent import AnonymousUserPatchError, PVDisabledError
7777
from renku_data_services.notebooks.errors.programming import ProgrammingError
78-
from renku_data_services.notebooks.errors.user import MissingResourceError, UserInputError
78+
from renku_data_services.notebooks.errors.user import MissingResourceError
7979
from renku_data_services.notebooks.util.kubernetes_ import (
8080
find_container,
8181
renku_1_make_server_name,
@@ -161,7 +161,7 @@ async def _user_server(
161161
) -> JSONResponse:
162162
server = await self.nb_config.k8s_client.get_server(server_name, user.id)
163163
if server is None:
164-
raise MissingResourceError(message=f"The server {server_name} does not exist.")
164+
raise errors.MissingResourceError(message=f"The server {server_name} does not exist.")
165165
server = UserServerManifest(server, self.nb_config.sessions.default_image)
166166
return json(NotebookResponse().dump(server))
167167

@@ -350,14 +350,14 @@ async def launch_notebook_helper(
350350
if is_image_private and internal_gitlab_user.access_token:
351351
image_repo = image_repo.with_oauth2_token(internal_gitlab_user.access_token)
352352
if not image_repo.image_exists(parsed_image):
353-
raise MissingResourceError(
353+
raise errors.MissingResourceError(
354354
message=(
355355
f"Cannot start the session because the following the image {image} does not "
356356
"exist or the user does not have the permissions to access it."
357357
)
358358
)
359359
else:
360-
raise UserInputError(message="Cannot determine which Docker image to use.")
360+
raise errors.ValidationError(message="Cannot determine which Docker image to use.")
361361

362362
parsed_server_options: ServerOptions | None = None
363363
if resource_class_id is not None:
@@ -385,7 +385,7 @@ async def launch_notebook_helper(
385385
# The old style API was used, try to find a matching class from the CRC service
386386
parsed_server_options = await nb_config.crc_validator.find_acceptable_class(user, requested_server_options)
387387
if parsed_server_options is None:
388-
raise UserInputError(
388+
raise errors.ValidationError(
389389
message="Cannot find suitable server options based on your request and "
390390
"the available resource classes.",
391391
detail="You are receiving this error because you are using the old API for "
@@ -397,8 +397,8 @@ async def launch_notebook_helper(
397397
default_resource_class = await nb_config.crc_validator.get_default_class()
398398
max_storage_gb = default_resource_class.max_storage
399399
if storage is not None and storage > max_storage_gb:
400-
raise UserInputError(
401-
"The requested storage amount is higher than the "
400+
raise errors.ValidationError(
401+
message="The requested storage amount is higher than the "
402402
f"allowable maximum for the default resource class of {max_storage_gb}GB."
403403
)
404404
if storage is None:
@@ -434,14 +434,16 @@ async def launch_notebook_helper(
434434
)
435435
)
436436
except ValidationError as e:
437-
raise UserInputError(f"Couldn't load cloud storage config: {str(e)}")
437+
raise errors.ValidationError(message=f"Couldn't load cloud storage config: {str(e)}")
438438
mount_points = set(s.mount_folder for s in storages if s.mount_folder and s.mount_folder != "/")
439439
if len(mount_points) != len(storages):
440-
raise UserInputError(
441-
"Storage mount points must be set, can't be at the root of the project and must be unique."
440+
raise errors.ValidationError(
441+
message="Storage mount points must be set, can't be at the root of the project and must be unique."
442442
)
443443
if any(s1.mount_folder.startswith(s2.mount_folder) for s1 in storages for s2 in storages if s1 != s2):
444-
raise UserInputError("Cannot mount a cloud storage into the mount point of another cloud storage.")
444+
raise errors.ValidationError(
445+
message="Cannot mount a cloud storage into the mount point of another cloud storage."
446+
)
445447

446448
repositories = repositories or []
447449

@@ -479,7 +481,7 @@ async def launch_notebook_helper(
479481
)
480482

481483
if len(server.safe_username) > 63:
482-
raise UserInputError(
484+
raise errors.ValidationError(
483485
message="A username cannot be longer than 63 characters, "
484486
f"your username is {len(server.safe_username)} characters long.",
485487
detail="This can occur if your username has been changed manually or by an admin.",
@@ -557,7 +559,9 @@ async def _patch_server(
557559
state = PatchServerStatusEnum.from_api_state(body.state) if body.state is not None else None
558560
resource_class_id = patch_body.resource_class_id
559561
if server and not (currently_hibernated or currently_failing) and resource_class_id:
560-
raise UserInputError("The resource class can be changed only if the server is hibernated or failing")
562+
raise errors.ValidationError(
563+
message="The resource class can be changed only if the server is hibernated or failing"
564+
)
561565

562566
if resource_class_id:
563567
parsed_server_options = await self.nb_config.crc_validator.validate_class_storage(
@@ -704,12 +708,9 @@ def stop_server(self) -> BlueprintFactoryResponse:
704708

705709
@authenticate(self.authenticator)
706710
async def _stop_server(
707-
request: Request, user: AnonymousAPIUser | AuthenticatedAPIUser, server_name: str
711+
_: Request, user: AnonymousAPIUser | AuthenticatedAPIUser, server_name: str
708712
) -> HTTPResponse:
709-
try:
710-
await self.nb_config.k8s_client.delete_server(server_name, safe_username=user.id)
711-
except MissingResourceError as err:
712-
raise exceptions.NotFound(message=err.message)
713+
await self.nb_config.k8s_client.delete_server(server_name, safe_username=user.id)
713714
return HTTPResponse(status=204)
714715

715716
return "/notebooks/servers/<server_name>", ["DELETE"], _stop_server
@@ -748,7 +749,7 @@ async def _server_logs(
748749
)
749750
return json(ServerLogs().dump(logs))
750751
except MissingResourceError as err:
751-
raise exceptions.NotFound(message=err.message)
752+
raise errors.MissingResourceError(message=err.message)
752753

753754
return "/notebooks/logs/<server_name>", ["GET"], _server_logs
754755

test/bases/renku_data_services/data_api/test_notebooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async def test_server_options(sanic_client: SanicASGITestClient, user_headers):
183183

184184
@pytest.mark.asyncio
185185
@pytest.mark.parametrize(
186-
"server_name_fixture,expected_status_code", [("unknown_server_name", 404), ("server_name", 204)]
186+
"server_name_fixture,expected_status_code", [("unknown_server_name", 204), ("server_name", 204)]
187187
)
188188
async def test_stop_server(
189189
sanic_client: SanicASGITestClient,

0 commit comments

Comments
 (0)