Skip to main content

Presentable Exceptions for Django Restframework Projects

Project description

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).

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

django-presentable-exception-0.0.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-presentable-exception-0.0.1.tar.gz.

File metadata

  • Download URL: django-presentable-exception-0.0.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-presentable-exception-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c02bfc33221698de5b9186baafad6a2dd7aa6b834c866b67b91e581f71797a31
MD5 1dd9198cc621c41032ebd95faef9233c
BLAKE2b-256 15e255045e69d574a906a9307a71aa958bac9c67a1e3d0da379e86819c1d0aca

See more details on using hashes here.

File details

Details for the file django_presentable_exception-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_presentable_exception-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_presentable_exception-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5d0ff2ccfec39d88c8bef6a46b402b59e41ce1fc47e997fd277ae98eed0b4ff
MD5 6576612579ae1eafebf3cc6f863fa167
BLAKE2b-256 eab251ba05f4ebe955102b6378d430a483f6cb5fa885a21a9b9852ce5c5c5e46

See more details on using hashes here.

Supported by

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