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."
}
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.
}
Testing
Install dependencies via poetry.
poetry install
Run tests.
pytest test_project
Client SDKs
We have SDKs for client side.
hipo-exceptions-jsis for JS.hipo-exceptions-androidis for Android.
Support
Please open an issue for support.
Contributing
Please contribute using Github Flow. Create a branch, add commits, and open a pull request.
Metadata
Release files for hipo-drf-exceptions 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hipo-drf-exceptions-0.1.8.tar.gz | 9.0 kB | Without provenance | Details for hipo-drf-exceptions-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hipo_drf_exceptions-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for hipo_drf_exceptions-0.1.8-py3-none-any.whl |
Total release size: 22.7 kB
Release files / hipo-drf-exceptions-0.1.8.tar.gz
| Download URL | hipo-drf-exceptions-0.1.8.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f26c0c21bba5cd1e0cb5fc934160576a68ec3979f401799056c8914490873b4
|
|
BLAKE2b-256 checksum How to use checksums |
f858c1ef6c4433370772b5a0b1029e0b6fc37ec158b8091f0aa0aa1c30861615
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.9.9 Darwin/21.1.0
|
Release files / hipo_drf_exceptions-0.1.8-py3-none-any.whl
| Download URL | hipo_drf_exceptions-0.1.8-py3-none-any.whl |
|---|---|
| Size | 13.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0f051d482385c7084e9ea187090f1712b112e850df3b631872c9962976333d6b
|
|
BLAKE2b-256 checksum How to use checksums |
9d84a73b395d555e91f0a5c17cacf73e251ebe883a20f21e06b2fea788fc2a0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.9.9 Darwin/21.1.0
|
Security for hipo-drf-exceptions 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.