haystack/haystack/errors.py
Julian Risch ac5617e757
Add basic telemetry features (#2314)
* add basic telemetry features

* change pipeline_config to _component_config

* Update Documentation & Code Style

* add super().__init__() calls to error classes

* make posthog mock work with python 3.7

* Update Documentation & Code Style

* update link to docs web page

* log exceptions, send event for raised HaystackErrors, refactor Path(CONFIG_PATH)

* add comment on send_event in BaseComponent.init() and fix mypy

* mock NonPrivateParameters and fix pylint undefined-variable

* Update Documentation & Code Style

* check model path contains multiple /

* add test for writing to file

* add test for en-/disable telemetry

* Update Documentation & Code Style

* merge file deletion methods and ignore pylint global statement

* Update Documentation & Code Style

* set env variable in demo to activate telemetry

* fix mock of HAYSTACK_TELEMETRY_ENABLED

* fix mypy and linter

* add CI as env variable to execution contexts

* remove threading, add test for custom error event

* Update Documentation & Code Style

* simplify config/log file deletion

* add test for final event being sent

* force writing config file in test

* make test compatible with python 3.7

* switch to posthog production server

* Update Documentation & Code Style

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-03-21 11:58:51 +01:00

77 lines
2.4 KiB
Python

# coding: utf8
"""Custom Errors for Haystack"""
from haystack.telemetry import send_custom_event
from typing import Optional
class HaystackError(Exception):
"""
Any error generated by Haystack.
This error wraps its source transparently in such a way that its attributes
can be accessed directly: for example, if the original error has a `message` attribute,
`HaystackError.message` will exist and have the expected content.
"""
def __init__(self, message: Optional[str] = None, docs_link: Optional[str] = None):
send_custom_event(event=f"{type(self).__name__} raised")
super().__init__()
if message:
self.message = message
self.docs_link = None
def __getattr__(self, attr):
# If self.__cause__ is None, it will raise the expected AttributeError
getattr(self.__cause__, attr)
def __str__(self):
if self.docs_link:
docs_message = f"\n\nCheck out the documentation at {self.docs_link}"
return self.message + docs_message
return self.message
def __repr__(self):
return str(self)
class PipelineError(HaystackError):
"""Exception for issues raised within a pipeline"""
def __init__(
self, message: Optional[str] = None, docs_link: Optional[str] = "https://haystack.deepset.ai/pipelines"
):
super().__init__(message=message, docs_link=docs_link)
class PipelineSchemaError(PipelineError):
"""Exception for issues arising when reading/building the JSON schema of pipelines"""
def __init__(self, message: Optional[str] = None):
super().__init__(message=message)
class PipelineConfigError(PipelineError):
"""Exception for issues raised within a pipeline's config file"""
def __init__(
self,
message: Optional[str] = None,
docs_link: Optional[str] = "https://haystack.deepset.ai/pipelines#yaml-file-definitions",
):
super().__init__(message=message, docs_link=docs_link)
class DocumentStoreError(HaystackError):
"""Exception for issues that occur in a document store"""
def __init__(self, message: Optional[str] = None):
super().__init__(message=message)
class DuplicateDocumentError(DocumentStoreError, ValueError):
"""Exception for Duplicate document"""
def __init__(self, message: Optional[str] = None):
super().__init__(message=message)