The Django REST Framework Exception Handler is a Python package designed to streamline exception handling within Django REST Framework applications. It ensures a consistent format for error responses by structuring them in a predefined JSON format.
Project description
Django REST Framework Exception Handler
The Django REST Framework Exception Handler is a Python package designed to streamline exception handling within Django REST Framework applications. It ensures a consistent format for error responses by structuring them in a predefined JSON format. Installation
You can install the package via pip:
pip install django-rest-exception-handler
Usage
Installation: After installation, include the package in your Django REST Framework settings in settings.py
.
REST_FRAMEWORK = {
# Other settings...
'EXCEPTION_HANDLER': 'django_rest_exception_handler.exception_handlers.exception_handler'
}
Response Format: The package handles exceptions and formats the response in the following structure:
{
"status": false,
"message": "fail",
"error": "specific error message here"
}
With using this package you could raise an excetion anywhere inside your application. Exceptions other than the APIException class will be raised as 500 Internal server error exception by the exception handler.
Example: Here the exception handler will raise a 400 exception if the data passed by the request to the serializer is invalid.
# Your Django view code
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.request import Request
from .serializers import YourSerializer
class YourView(APIView):
def post(self, request: Request) -> Response:
serializer = YourSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
Contributing
We welcome contributions! Feel free to open issues or submit pull requests. License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
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 django_rest_exception_handler-1.1.tar.gz
.
File metadata
- Download URL: django_rest_exception_handler-1.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f666ba0cf196c5248a491486e39df61fa0e014d204415c39ca2d513a8938f720 |
|
MD5 | 27bde0f325db1cf5dd079626f4c38a05 |
|
BLAKE2b-256 | f8bf1df6cf5be41f40b438bf4a0feb5f34c4a53398235739ff63b5cb9b1bfe0c |
File details
Details for the file django_rest_exception_handler-1.1-py3-none-any.whl
.
File metadata
- Download URL: django_rest_exception_handler-1.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32e8f1102fabfb5f97895f3f214e12a3d753d24335bc89ec0f4736e2e25bac24 |
|
MD5 | c6ac9431a9c3dfe5896cce6a877f1710 |
|
BLAKE2b-256 | 02c746ab3099396f262ea752996ddc5b45e57ec97c941619bf00edc81d212481 |