From ebd4403c2f74bf6f65f717ad389b89e869a7efc2 Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Wed, 13 Aug 2025 12:14:42 +0200 Subject: [PATCH] adds new error classes to keywordextration error and defines new error --- .../data/exceptions/__init__.py | 9 ++++++++ .../data/exceptions/exceptions.py | 22 +++++++++++++++++++ .../data/utils/extract_keywords.py | 4 ++-- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 cognee/infrastructure/data/exceptions/__init__.py create mode 100644 cognee/infrastructure/data/exceptions/exceptions.py diff --git a/cognee/infrastructure/data/exceptions/__init__.py b/cognee/infrastructure/data/exceptions/__init__.py new file mode 100644 index 000000000..6735200ed --- /dev/null +++ b/cognee/infrastructure/data/exceptions/__init__.py @@ -0,0 +1,9 @@ +""" +Custom exceptions for the Cognee API. + +This module defines a set of exceptions for handling various data errors +""" + +from .exceptions import ( + KeywordExtractionError +) diff --git a/cognee/infrastructure/data/exceptions/exceptions.py b/cognee/infrastructure/data/exceptions/exceptions.py new file mode 100644 index 000000000..5c36d6754 --- /dev/null +++ b/cognee/infrastructure/data/exceptions/exceptions.py @@ -0,0 +1,22 @@ +from cognee.exceptions import ( + CogneeValidationError, +) +from fastapi import status + + +class KeywordExtractionError(CogneeValidationError): + """ + Raised when a provided value is syntactically valid but semantically unacceptable + for the given operation. + + Example: + - Passing an empty string to a keyword extraction function. + """ + + def __init__( + self, + message: str = "Extract_keywords cannot extract keywords from empty text.", + name: str = "KeywordExtractionError", + status_code: int = status.HTTP_400_BAD_REQUEST, + ): + super().__init__(message, name, status_code) diff --git a/cognee/infrastructure/data/utils/extract_keywords.py b/cognee/infrastructure/data/utils/extract_keywords.py index bd4fedd56..2915131a4 100644 --- a/cognee/infrastructure/data/utils/extract_keywords.py +++ b/cognee/infrastructure/data/utils/extract_keywords.py @@ -1,6 +1,6 @@ from sklearn.feature_extraction.text import TfidfVectorizer -from cognee.exceptions import InvalidValueError +from cognee.infrastructure.data.exceptions.exceptions import KeywordExtractionError from cognee.shared.utils import extract_pos_tags @@ -25,7 +25,7 @@ def extract_keywords(text: str) -> list[str]: with more than 3 characters. """ if len(text) == 0: - raise InvalidValueError(message="extract_keywords cannot extract keywords from empty text.") + raise KeywordExtractionError() tags = extract_pos_tags(text) nouns = [word for (word, tag) in tags if tag == "NN"]