Skip to main content

Provides primitives for application errors

Project description

Classic Error Handling

Этот пакет предоставляет примитивы для обработки ошибок на уровне приложения. Часть проекта "Classic".

Установка

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.1.tar.gz (5.2 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.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: classic_error_handling-0.2.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for classic_error_handling-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b90284e7485da02dfb70c48e4d9bef4222f3fb5127e80b14567ce94e6935c7e4
MD5 a09d432330eb9d9f38786a11538923e6
BLAKE2b-256 bfc957150fa15332a67cdbde14d9b55602bd7065eadb2b42570aad6f286f14d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for classic_error_handling-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1be0cbb77946de0298a17f25fa7fbf9a263736c76402695baa2b6f35f00ea005
MD5 c22da618899559f24c2486b6a7b877af
BLAKE2b-256 c082ca5e7447b29c8fa03ab36a3588e421b5ecfb54155e3a0cb4a3897d8f6d09

See more details on using hashes here.

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