Skip to main content

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 = Post

    def validate_title(self, value):
        if value[0] != value[0].upper():
            raise ValidationError('First letter must be an uppercase')
        return value

    def 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 attrs

    FIELD_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 code

If 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 = Post

    def 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 attrs

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'
}

Metadata

Release files for drf-errors 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-errors 0.1.4
File Size Uploaded
drf_errors-0.1.4.tar.gz 3.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-errors 0.1.4
File Interpreter ABI Platform
drf_errors-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / drf_errors-0.1.4.tar.gz

Download URL drf_errors-0.1.4.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4e96faee578cf7efc371ad553f71af730019a6f608b8f2037b0eb638da23e940
BLAKE2b-256 checksum
How to use checksums
50b51b68d9441310cdb671d46b3b4c7e0f6265e175cd0cc53fe885afb7dfc750
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / drf_errors-0.1.4-py3-none-any.whl

Download URL drf_errors-0.1.4-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bed46928ca34f4c9d0da832b092d4b6d079ea10c5f6d3160f9762d05f7c3c29a
BLAKE2b-256 checksum
How to use checksums
8bac3f13b8407a4dbfba7474fbf1824e3219186828cc620f04afe24c8b7f435a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page