{"id":21832891,"url":"https://github.com/null-none/drf-errors","last_synced_at":"2025-04-14T07:37:43.125Z","repository":{"id":49214542,"uuid":"258304326","full_name":"null-none/drf-errors","owner":"null-none","description":"Extension for Django REST framework error display","archived":false,"fork":false,"pushed_at":"2022-09-16T18:50:17.000Z","size":49,"stargazers_count":18,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T21:12:01.927Z","etag":null,"topics":["api","api-client","django","drf","rest","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/null-none.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null}},"created_at":"2020-04-23T19:06:12.000Z","updated_at":"2023-07-20T23:43:31.000Z","dependencies_parsed_at":"2022-09-14T07:52:50.718Z","dependency_job_id":null,"html_url":"https://github.com/null-none/drf-errors","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/null-none%2Fdrf-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/null-none%2Fdrf-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/null-none%2Fdrf-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/null-none%2Fdrf-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/null-none","download_url":"https://codeload.github.com/null-none/drf-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248840560,"owners_count":21170016,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-client","django","drf","rest","rest-api"],"created_at":"2024-11-27T19:26:51.118Z","updated_at":"2025-04-14T07:37:43.092Z","avatar_url":"https://github.com/null-none.png","language":"Python","readme":"DRF Errors\n===================\n\n**Extension for Django REST framework error display**\n\nRequirements\n------------\n-  Python (2.7, 3.5, 3.6)\n-  Django (3.0.6+)\n-  Django REST framework (\u003e=3.5)\n\nInstallation\n------------\n\nBy running installation script\n\nUsing pip\n\n.. code:: bash\n\n    $ pip install drf-errors\n\n\nOverview\n--------\n\nThis package extends default error JSON body providing configurable error codes\nand more consumable response structure.\n\nIt turns default JSON body of HTTP 400 response, which look like this\n\n.. code:: python\n\n    {\n        \"name\": [\"This field is required.\"],\n        \"password\": [\"This field may not be blank.\"]\n    }\n\ninto\n\n.. code:: python\n\n    {\n      \"message\": \"Email: This field is required.\",\n      \"errors\": [\n        {\n          \"field\": \"email\",\n          \"message\": \"This field is required.\"\n        },\n        {\n          \"field\": \"password\",\n          \"message\": \"This field is required.\"\n        }\n      ],\n      \"status_code\": 400\n    }\n\nUsage\n-----\n\nSimply add a SerializerErrorMessagesMixin to your serializer or model serializer class\n\n.. code:: python\n\n    from drf_errors.mixins import SerializerErrorMessagesMixin\n\n    class MySerializer(SerializerErrorMessagesMixin, ModelSerializer):\n\nIf you want to change default library settings and provide your own set of error codes just add following in your\nsettings.py\n\n.. code:: python\n\n    DRF_ERRORS = {\n        FIELD_ERRORS = {\n            'CharField': {'required': 'my_custom_error_code', 'null': 'my_custom_error_code'}\n        }\n        VALIDATOR_ERRORS = {\n            'UniqueValidator': 'my_custom_error_code'\n        },\n        EXCEPTION_DICT = {\n            'PermissionDenied': 'my_custom_error_code'\n        }\n    }\n\nCustom serializer validation\n----------------------------\n\nIf you need custom field validation or validation for whole serializer register your validation in serializer class\n\n.. code:: python\n\n    class PostSerializer(SerializerErrorMessagesMixin,\n                         serializers.ModelSerializer):\n        class Meta:\n            model = Post\n\n        def validate_title(self, value):\n            if value[0] != value[0].upper():\n                raise ValidationError('First letter must be an uppercase')\n            return value\n\n        def validate(self, attrs):\n            category = attrs.get('category)\n            title = attrs.get('title')\n            if category and category not in title:\n                raise ValidationError('Title has to include category')\n            return attrs\n\n        FIELD_VALIDATION_ERRORS = {'validate_title': 'invalid_title'} # register your own validation method and assign it to error code\n        NON_FIELD_ERRORS = {'Title has to include category': 'no_category'} # register non field error messages and assign it to error code\n\nIf you want to raise field error in validate method use register_error method provided by a mixin\n\n.. code:: python\n\n    class PostSerializer(SerializerErrorMessagesMixin,\n                         serializers.ModelSerializer):\n        class Meta:\n            model = Post\n\n        def validate(self, attrs):\n            category = attrs.get('category')\n            title = attrs.get('title')\n            if category and category not in title:\n                self.register_error(error_message='Title has to include category',\n                                    error_code='no_category',\n                                    field_name='title')\n            return attrs\n\nError codes not related to serializer validation\n------------------------------------------------\n\nTo turn other type of errors responses into friendly errors responses with error codes\nadd this exception handler to your REST_FRAMEWORK settings\n\n.. code:: python\n\n    REST_FRAMEWORK = {\n        'EXCEPTION_HANDLER':\n        'drf_errors.handlers.drf_exception_handler'\n    }\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnull-none%2Fdrf-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnull-none%2Fdrf-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnull-none%2Fdrf-errors/lists"}