Package: drf-problems
DRF ProblemsTL;DRThis library implementsRFC 7807in our favorite Django REST Framework! Or, in layman terms, it introduces "Problem Details" in the HTTP APIs.Table of ContentsFeaturesPre-RequisitesInstallationUsageWith exceptionsWith permissionsWith ViewsResourcesRoadmapContributingFeaturesHandles exception to return response with Problem Details model.Added permission mixins and base class to store exception to raise by the view on failure of permission.Added view mixin which throws exception of failed permission.These permissions are compatible with composable permissions introduced in DRF 3.9.0!Has problem description endpoint to understand about the problem with the given code.Has sample permissions which checks for minimum API version.Pre-RequisitesDjango >= 2.0 (Tested with 2.2)djangorestframework >= 3.0.0 (Tested with 3.9.0)InstallationInstall the library as you would for any django library.Install using pip.pip install drf-problemsAdd 'drf_problems' to yourINSTALLED_APPSsetting.INSTALLED_APPS=(...'drf_problems',)DRF's default exception handler needs to be replaced. In yoursettings.py, update:REST_FRAMEWORK={...'EXCEPTION_HANDLER':'drf_problems.exceptions.exception_handler',To use the problem description url, you need to update yoururls.py:urlpatterns=[...path('',include('drf_problems.urls'))]UsageWith exceptionsIn your exception class, definedefault_codewith the error code string which is used in the type URI.
To set custom title, definetitlewith the human-readable summary of the problem type.
To set description, definedescriptionwith a long paragraph describing the problem.Finally, make sure to register your exception withdrf_problems.utils.register_exceptionfunction ordrf_problems.utils.registerdecorator.
Here's a sample exception class:fromdrf_problems.utilsimportregister_exception,register@register# Either use this decoratorclassInvalidVersionRequestedException(exceptions.NotAcceptable):default_code='invalid_version'title='Invalid API version'default_detail='Provided API version is invalid.')description='Malformed or unsupported version string is provided with the request.'register_exception(InvalidVersionRequestedException)# Or this method directly.With permissionsUse eitherdrf_problems.permissions.ProblemPermissionMixinmixin class with your existing permissions, or extend directly fromdrf_problems.permissions.BaseProblemPermission.
Defineexception_classin the permissions to the desired exception class.
For flexibility, you can even set exception instance by settingexceptionattribute on the permission object.Here's a sample permissions class:fromdrf_problems.permissionsimportBaseProblemPermissionclassMinimumVersionRequiredPermission(BaseProblemPermission):exception_class=InvalidVersionRequestedExceptionWith ViewsNote: The permissions wouldn't throw the desired exception from the view, until the view is extended from thedrf_problems.mixins.AllowPermissionWithExceptionViewMixinmixin. So, remember to update your views too, for which permissions are updated!ResourcesOfficial GuideProblem Details for HTTP APIsREST API Error Handling - Problem Details ResponseRoadmapAdd tests with some sample views using exceptions and permissions.Document the code better.ContributingContributions are very welcome, of any kind - whether finding new issues or any ideas for enhancements or a pull request.