Skip to main content

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

Parsing error messages generated by DRF is a bit of pain for client developers. They need to try-catch different possible error formats. When you add errors raised at the business logic level, the error parsing becomes even more difficult.

This package unifies the output format of DRF in the "Hipo Error Protocol".

No more "An error occured." errors.

This package also provides the "fallback message", a text string that always contains a human readable error summary. This way, client developers can always fallback and show this message when the client receives an error that is not handled.

Sounds cool! Can client devs just use this field all the time?

In our past experience, we noticed that some lazy client developers tend to use this message and avoid writing any code to parse the error bundle. However, the message in this field is automatically generated and may not always be suitable for end users. In order to make clear that this is a fallback message, we named this field "fallback_message"

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

You can make 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 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."
}

Settings

You can set default key for Django's non field errors key (it is "__all__") by adding this to your Django settings:

HIPO_DRF_EXCEPTIONS_SETTINGS = {
    "DJANGO_NON_FIELD_ERRORS_KEY": "field_free_errors"  # by default it's "non_field_errors" to be consistent with DRF.
}

Client SDKs

We have SDKs for client side.

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.6.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

hipo_drf_exceptions-0.1.6-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: hipo-drf-exceptions-0.1.6.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/2.7.16 Darwin/19.6.0

File hashes

Hashes for hipo-drf-exceptions-0.1.6.tar.gz
Algorithm Hash digest
SHA256 148451897b560c8fbc63e5b1d952c047a5db4ada94437a84550ee9ba7b4e9749
MD5 86b94c39e7ca3387750b0d95012c2daa
BLAKE2b-256 ba2656072f05b3a5df123334bd853d14dc2ae31be31fb027f50ad723e554e6e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hipo_drf_exceptions-0.1.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4475c9cc64f50b9fa3b4cde839ebf97f122389a89d3dd1f2515b0196a3c2935a
MD5 1ecc7f345f158e829e8153af9006bea1
BLAKE2b-256 3dc02787d0440f54fb2cc28610ee1957b6695068b8ac4ce50fb5513b77143c40

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