From f41b643d37124e76615af3aaf8fb39f65dd1d9f7 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Wed, 2 Oct 2024 14:30:20 +0200 Subject: [PATCH] Enhancement/improve-error-messaging-for-role-forbidden (#5554) # Description Closes # **Type of change** - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Refactor (change restructuring the codebase without changing functionality) - Improvement (change adding some improvement to an existing functionality) - Documentation update **How Has This Been Tested** **Checklist** - I added relevant documentation - I followed the style guidelines of this project - I did a self-review of my code - I made corresponding changes to the documentation - I confirm My changes generate no new warnings - I have added tests that prove my fix is effective or that my feature works - I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paco Aranda --- argilla/src/argilla/_exceptions/_api.py | 2 +- argilla/src/argilla/datasets/_resource.py | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/argilla/src/argilla/_exceptions/_api.py b/argilla/src/argilla/_exceptions/_api.py index 9f24bafc35..38c1b01176 100644 --- a/argilla/src/argilla/_exceptions/_api.py +++ b/argilla/src/argilla/_exceptions/_api.py @@ -36,7 +36,7 @@ class BadRequestError(ArgillaAPIError): class ForbiddenError(ArgillaAPIError): - message = "Forbidden request to the server" + message = "User role is forbidden from performing this action by server" class NotFoundError(ArgillaAPIError): diff --git a/argilla/src/argilla/datasets/_resource.py b/argilla/src/argilla/datasets/_resource.py index f631d32653..68391c4a8f 100644 --- a/argilla/src/argilla/datasets/_resource.py +++ b/argilla/src/argilla/datasets/_resource.py @@ -15,8 +15,13 @@ from typing import Optional, Union from uuid import UUID, uuid4 +try: + from typing import Self +except ImportError: + from typing_extensions import Self + from argilla._api import DatasetsAPI -from argilla._exceptions import NotFoundError, SettingsError +from argilla._exceptions import NotFoundError, SettingsError, ForbiddenError from argilla._models import DatasetModel from argilla._resource import Resource from argilla.client import Argilla @@ -157,7 +162,16 @@ def create(self) -> "Dataset": Returns: Dataset: The created dataset object. """ - super().create() + try: + super().create() + except ForbiddenError as e: + settings_url = f"{self._client.api_url}/user-settings" + user_role = self._client.me.role.value + user_name = self._client.me.username + workspace_name = self.workspace.name + message = f"""User '{user_name}' is not authorized to create a dataset in workspace '{workspace_name}' + with role '{user_role}'. Go to {settings_url} to view your role.""" + raise ForbiddenError(message) from e try: return self._publish() except Exception as e: @@ -277,3 +291,6 @@ def _sanitize_name(cls, name: str): for character in ["/", "\\", ".", ",", ";", ":", "-", "+", "="]: name = name.replace(character, "-") return name + + def _with_client(self, client: Argilla) -> "Self": + return super()._with_client(client=client)