A Django app for returning consistent, verbose and easy to parse error messages on Django Rest Framework backends.
Project description
Hipo DRF Exceptions
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."
}
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hipo-drf-exceptions-0.1.4.tar.gz
.
File metadata
- Download URL: hipo-drf-exceptions-0.1.4.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.16 CPython/2.7.15 Darwin/18.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b54c37c89ab8f51ae7951b73c000c42e83e6448cf7e8f553c4a2f455848fefd4 |
|
MD5 | fa9d01a5afd87705fbfebd2df25d9f81 |
|
BLAKE2b-256 | 3aa7786cf62e2570595785a3218eb50a95196a7b98b9fa6d5f4573c7a7fbc483 |
File details
Details for the file hipo_drf_exceptions-0.1.4-py2.py3-none-any.whl
.
File metadata
- Download URL: hipo_drf_exceptions-0.1.4-py2.py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.16 CPython/2.7.15 Darwin/18.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5ea912e9614665721a7e609273f55de475cd484191e3817c3f5000a61094406 |
|
MD5 | 2d12394f39570393888ae4ca28aef08c |
|
BLAKE2b-256 | 791352657784b1a6ca7a8ca559dbbed14b009ba843fa2acdd99397399630e3b5 |