Skip to main content

An Django app for returning consistent, verbose and easy to parse error messages on Django Rest Framework backends.

Project description

Hipo DRF Exceptions

hipo Build Status pypi

A Django app for returning consistent, verbose and easy to parse error messages on Django Rest Framework backends.

Table of Contents

Installation

You can get stable version of Hipo Excepitons by using pip, pipenv or poetry:

pip install hipo-drf-exceptions

Usage

Handler

You will need to set EXCEPTION_HANDLER of the REST_FRAMEWORK setting of your Django project settings.py file.

REST_FRAMEWORK = {
    ..
    'EXCEPTION_HANDLER': 'hipo_drf_exceptions.handler',
}

Example Error Responses

Field Error

Have validations on model level and raise ValidationError when it is required.

from django.core.exceptions import ValidationError

class Invitation(models.Model):
    email = models.EmailField(unique=True)

    def save(self, *args, **kwargs):
        if User.objects.filter(email=self.email).exists():
            raise ValidationError({"email": _("Email is already registered.")})
            
        super().save(*args, **kwargs)

If the view or serializer encounters with the ValidationError, The response will be like:

{
    "type": "ValidationError",
    "detail": {
        "email": [
            "Email is already registered."
        ]
    },
    "fallback_message": "'email' has an error. Email is already registered."
}

Non Field Error

Implement your own error classes.

from hipo_drf_exceptions import BaseAPIException

class ProfileCredentialError(BaseAPIException):
    default_detail = _('Profile credentials are not correct.')

Raise error when it is required.

class AuthenticationView(GenericAPIView):

    def post(self, request, *args, **kwargs):
        ..
        if not profile.check_password(password):
            raise ProfileCredentialError()
        ..

The response will be like:

{
    "type": "ProfileCredentialError",
    "detail": {
        "non_field_errors": [
            "Profile credentials are not correct."
        ]
    },
    "fallback_message": "Profile credentials are not correct."
}

Support

Please open an issue for support.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

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

hipo-drf-exceptions-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

hipo_drf_exceptions-0.1.0-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file hipo-drf-exceptions-0.1.0.tar.gz.

File metadata

  • Download URL: hipo-drf-exceptions-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.16 CPython/2.7.15 Darwin/18.2.0

File hashes

Hashes for hipo-drf-exceptions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6c81901bbc8972d22c15eec37bb52e00306c82153df67d06fc1faa979caa6cd0
MD5 42f5fe16437e9df2ab9d27aa2bc3b41b
BLAKE2b-256 b2ffd5b4266d0928238525545d6b0291529dd01cc2b893810df72db38d96d33e

See more details on using hashes here.

File details

Details for the file hipo_drf_exceptions-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for hipo_drf_exceptions-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 73534f5b02951ab75068dfcc72914ffd71f1fbbe91332603a181b74e56caf6af
MD5 1734f4a2afab1924533a1775bcc2792a
BLAKE2b-256 c0ce02800f39c5d7aba110cd741b2d783c0355009ed2d88bab82ba7a9353ff5b

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