Skip to main content

Provides primitives for application errors

Project description

Classic Error Handling

Пакет предоставляет примитивы для обработки ошибок на уровне приложения.

Установка

pip install classic-error-handling

Использование

Примеры оформления ошибок:

from classic.error_handling import Error, ErrorsList


# Ошибка с пространством имен
class Namespaced(Error):
    namespace = 'some_namespace'


# Наследование пространства имен
class InheritingNamespace(Namespaced):
    pass


# Ошибка с пространством имен и явным кодом
class NamespacedWithCode(Error):
    namespace = 'some_namespace'
    code = 'some_error'


# Ошибка с явным кодом без пространства имен
class WithCode(Error):
    code = 'some_error'


# Ошибка с литеральным сообщением
class WithLiteralMessage(Error):
    message_template = 'some_message'


# Ошибка с шаблоном сообщения
class WithMessageTemplate(Error):
    message_template = 'some_template: {arg}'

Пример использования:

from classic.error_handling import Error, ErrorsList


# Определяем ошибки приложения
class IncorrectState(Error):
    namespace = 'app'
    message_template = 'Некорректное состояние приложения - "{text}"'


class ServiceNotReady(Error):
    namespace = 'app'
    message_template = 'Сервис еще не готов'


# В сервисах:
class SomeService:
    
    def __init__(self):
        self.ready_to_serve = False

    def is_ready(self):
        """Демонстрирует простое использование"""
        if not self.ready_to_serve:
            raise ServiceNotReady()

    def mark_as_ready(self):
        """Демонстрирует использование шаблонов сообщений"""
        if self.ready_to_serve:
            raise IncorrectState(text='Сервис уже готов')
        self.ready_to_serve = True

    def just_give_errors(self):
        """Демонстрирует метод, который может вернуть несколько ошибок"""
        errors = [IncorrectState(text='ошибка 1'), 
                  IncorrectState(text='ошибка 2')]
        raise ErrorsList(*errors)


# Где-то в адаптерах:

service = SomeService()

try:
    service.is_ready()
except Error as error:
    print(f'Приложение ответило с кодом ошибки "{error.code_representation}", '
          f'сообщение: "{error.message}"')

try:
    service.mark_as_ready()
    service.mark_as_ready()
except Error as error:
    print(f'Приложение ответило с кодом ошибки "{error.code_representation}", '
          f'сообщение: "{error.message}"')
    
try:
    service.just_give_errors()
except ErrorsList as errors_list:
    for error in errors_list.errors:
        print(f'Приложение ответило с кодом ошибки "{error.code_representation}", '
              f'сообщение: "{error.message}"')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classic_error_handling-0.2.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classic_error_handling-0.2.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file classic_error_handling-0.2.3.tar.gz.

File metadata

  • Download URL: classic_error_handling-0.2.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for classic_error_handling-0.2.3.tar.gz
Algorithm Hash digest
SHA256 8dad44e70fb99164d76010792800926389bd96b4da6903cca14fa0564330dbd0
MD5 657216323aae8991556c1587ea67936e
BLAKE2b-256 fb2b89a10233c899bff2d081c78d4502120a65896dc653a8fa2cf770e41fbd6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for classic_error_handling-0.2.3.tar.gz:

Publisher: publish.yml on variasov/classic-error-handling

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file classic_error_handling-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for classic_error_handling-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 43be68d377b803d73bf8eb2a734c41e0721ff7c3d94a5f4f121b7d3702aaf3f3
MD5 f743bd11d641e4c47ab04991197305a3
BLAKE2b-256 c12fe2b4fda32a5fc3e030ebd8702a431d25b8f041570d955d1953dba07cb76e

See more details on using hashes here.

Provenance

The following attestation bundles were made for classic_error_handling-0.2.3-py3-none-any.whl:

Publisher: publish.yml on variasov/classic-error-handling

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page