Package: hipo-drf-exceptions
Hipo DRF ExceptionsADjangoapp for returning consistent, verbose and easy to parse error messages onDjango Rest Frameworkbackends.Parsing error messages generated by DRF is a bit of pain for client developers. They need to try-catch different possible error formats. When you add errors raised at the business logic level, the error parsing becomes even more difficult.This package unifies the output format of DRF in the "Hipo Error Protocol".No more "An error occured." errors.This package also provides the "fallback message", a text string that always contains a human readable error summary. This way, client developers can always fallback and show this message when the client receives an error that is not handled.Sounds cool! Can client devs just use this field all the time?In our past experience, we noticed that somelazyclient developers tend to use this message and avoid writing any code to parse the error bundle. However, the message in this field is automatically generated and may not always be suitable for end users. In order to make clear that this is afallbackmessage, we named this field "fallback_message"Table of ContentsInstallationUsageClient SDKsSupportContributingInstallationYou can get stable version of Hipo Excepitons by using pip, pipenv or poetry:pip install hipo-drf-exceptionsUsageHandlerYou will need to setEXCEPTION_HANDLERof theREST_FRAMEWORKsetting of your Django project settings.py file.REST_FRAMEWORK = {
    ..
    'EXCEPTION_HANDLER': 'hipo_drf_exceptions.handler',
}Example Error ResponsesField ErrorYou can make validations on model level and raiseValidationErrorwhen it is required.fromdjango.core.exceptionsimportValidationErrorclassInvitation(models.Model):email=models.EmailField(unique=True)defsave(self,*args,**kwargs):ifUser.objects.filter(email=self.email).exists():raiseValidationError({"email":_("Email is already registered.")})super().save(*args,**kwargs)If the view or serializer encounters with theValidationError, The response will be like:{"type":"ValidationError","detail":{"email":["Email is already registered."]},"fallback_message":"Email is already registered."}Non Field ErrorImplement your own error classes.fromhipo_drf_exceptionsimportBaseAPIExceptionclassProfileCredentialError(BaseAPIException):default_detail=_('Profile credentials are not correct.')Raise error when it is required.classAuthenticationView(GenericAPIView):defpost(self,request,*args,**kwargs):..ifnotprofile.check_password(password):raiseProfileCredentialError()..The response will be like:{"type":"ProfileCredentialError","detail":{"non_field_errors":["Profile credentials are not correct."]},"fallback_message":"Profile credentials are not correct."}SettingsYou can set default key for Django's non field errors key (it is"__all__") by adding this to your Django settings:HIPO_DRF_EXCEPTIONS_SETTINGS = {
    "DJANGO_NON_FIELD_ERRORS_KEY": "field_free_errors"  # by default it's "non_field_errors" to be consistent with DRF.
}TestingInstall dependencies via poetry.poetry installRun tests.pytest test_projectClient SDKsWe have SDKs for client side.hipo-exceptions-jsis for JS.hipo-exceptions-androidis for Android.SupportPleaseopen an issuefor support.ContributingPlease contribute usingGithub Flow. Create a branch, add commits, andopen a pull request.