Skip to main content

Presentable Exceptions

This is a module for django restframework, in which modules can specifiy exceptions in an extra file with an key an description. At some place in the code, they can be fetched by the package and key and bubble up to the view. Here a mixin detects them and creates a proper response for an fat-client, which can show them then to to user - thus the exceptions are presentable.

Getting started

Say, you have a module, like auth_api which is registered in INSTALLED_APPS in the Django settings.py. Create a new file auth_api/presentable_exceptions.py with this content

presentable_exceptions = {
    'client': [
        ('not-enough-data-to-delete-user', 'You did not provide enough data, that we can delete the user {username}')
    ],
    'server': [
        ('backend-connection-lost', 'We lost the connection to the backend'),
    ]
}

This file can be in every module and consists of two parts: The exceptions, that are the clients fault and the exceptions that are our fault. The first will cause a 4xx error, the latter a 5xx.

Say, you have now an View somewhere, like that:

class MeView(WithPresentableException, APIView):

    def get(self, request):
        data = some_function_with_a_deep_call_stack()
        return Response(data)

This will call multiple function and somewhere there is the function boom() called:

def boom():
    raise PresentableException.of('auth_api', 'backend-connection-lost')


def some_function_with_a_deep_call_stack():
    # ...
    boom()
    # ...

This PresentableException will be an PresentableServerException and bubble up into the Mixing WithPresentableException specified in the view above, which generates a proper response (with a 5xx code in this case).

Metadata

Release files for django-presentable-exception 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-presentable-exception 0.0.1
File Size Uploaded
django-presentable-exception-0.0.1.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-presentable-exception 0.0.1
File Interpreter ABI Platform
django_presentable_exception-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / django-presentable-exception-0.0.1.tar.gz

Download URL django-presentable-exception-0.0.1.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c02bfc33221698de5b9186baafad6a2dd7aa6b834c866b67b91e581f71797a31
BLAKE2b-256 checksum
How to use checksums
15e255045e69d574a906a9307a71aa958bac9c67a1e3d0da379e86819c1d0aca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

Release files / django_presentable_exception-0.0.1-py3-none-any.whl

Download URL django_presentable_exception-0.0.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5d0ff2ccfec39d88c8bef6a46b402b59e41ce1fc47e997fd277ae98eed0b4ff
BLAKE2b-256 checksum
How to use checksums
eab251ba05f4ebe955102b6378d430a483f6cb5fa885a21a9b9852ce5c5c5e46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page