Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/null-none/drf-errors
Extension for Django REST framework error display
https://github.com/null-none/drf-errors
api api-client django drf rest rest-api
Last synced: about 1 month ago
JSON representation
Extension for Django REST framework error display
- Host: GitHub
- URL: https://github.com/null-none/drf-errors
- Owner: null-none
- License: mit
- Created: 2020-04-23T19:06:12.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-16T18:50:17.000Z (over 2 years ago)
- Last Synced: 2024-11-16T04:00:24.240Z (about 2 months ago)
- Topics: api, api-client, django, drf, rest, rest-api
- Language: Python
- Homepage:
- Size: 47.9 KB
- Stars: 18
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
DRF Errors
===================**Extension for Django REST framework error display**
Requirements
------------
- Python (2.7, 3.5, 3.6)
- Django (3.0.6+)
- Django REST framework (>=3.5)Installation
------------By running installation script
Using pip
.. code:: bash
$ pip install drf-errors
Overview
--------This package extends default error JSON body providing configurable error codes
and more consumable response structure.It turns default JSON body of HTTP 400 response, which look like this
.. code:: python
{
"name": ["This field is required."],
"password": ["This field may not be blank."]
}into
.. code:: python
{
"message": "Email: This field is required.",
"errors": [
{
"field": "email",
"message": "This field is required."
},
{
"field": "password",
"message": "This field is required."
}
],
"status_code": 400
}Usage
-----Simply add a SerializerErrorMessagesMixin to your serializer or model serializer class
.. code:: python
from drf_errors.mixins import SerializerErrorMessagesMixin
class MySerializer(SerializerErrorMessagesMixin, ModelSerializer):
If you want to change default library settings and provide your own set of error codes just add following in your
settings.py.. code:: python
DRF_ERRORS = {
FIELD_ERRORS = {
'CharField': {'required': 'my_custom_error_code', 'null': 'my_custom_error_code'}
}
VALIDATOR_ERRORS = {
'UniqueValidator': 'my_custom_error_code'
},
EXCEPTION_DICT = {
'PermissionDenied': 'my_custom_error_code'
}
}Custom serializer validation
----------------------------If you need custom field validation or validation for whole serializer register your validation in serializer class
.. code:: python
class PostSerializer(SerializerErrorMessagesMixin,
serializers.ModelSerializer):
class Meta:
model = Postdef validate_title(self, value):
if value[0] != value[0].upper():
raise ValidationError('First letter must be an uppercase')
return valuedef validate(self, attrs):
category = attrs.get('category)
title = attrs.get('title')
if category and category not in title:
raise ValidationError('Title has to include category')
return attrsFIELD_VALIDATION_ERRORS = {'validate_title': 'invalid_title'} # register your own validation method and assign it to error code
NON_FIELD_ERRORS = {'Title has to include category': 'no_category'} # register non field error messages and assign it to error codeIf you want to raise field error in validate method use register_error method provided by a mixin
.. code:: python
class PostSerializer(SerializerErrorMessagesMixin,
serializers.ModelSerializer):
class Meta:
model = Postdef validate(self, attrs):
category = attrs.get('category')
title = attrs.get('title')
if category and category not in title:
self.register_error(error_message='Title has to include category',
error_code='no_category',
field_name='title')
return attrsError codes not related to serializer validation
------------------------------------------------To turn other type of errors responses into friendly errors responses with error codes
add this exception handler to your REST_FRAMEWORK settings.. code:: python
REST_FRAMEWORK = {
'EXCEPTION_HANDLER':
'drf_errors.handlers.drf_exception_handler'
}