In https://code.djangoproject.com/ticket/31239 I proposed to create 
QueryDictKeyError derived from MultiValueDictKeyError and raise this 
exception on a missing parameter in request.GET and request.POST.

It is to be done to handle error in user data easily:

try:
 x = request.GET['x']
except MultiValueDictError:
 return response("Missing user data 'x'", status=404)
try:
 y = request.GET['y']
except MultiValueDictError:
 return response("Missing user data 'y'", status=404)

is much more cumbersome and error-prone than:

def handle_exception(self, exc):
  if isinstance(exc, QueryDictKeyError):
    # It is an unwise assumption that this is necessarily missing HTTP 
param,
    # but rewriting it in other way would be time consuming (and maybe even 
more error prone).
    return MyErrorResponse({"code": "PAR_1", "message": "Missing param.", 
"field": exc.args[0]})

The latter may be added to a base view to handle such errors in the entire 
project easily.
This handle_exception() could be even integrated into the core of Dajngo to 
handle such errors automatically without the user writing this code 
manually repeatedly.
Moreover, the above code is much more maintenable, as no need to change 
error messages in each and every view, if a user wants to change the error 
message.

Programming is about automation and we need the task about handling user 
input errors to be done automatically.

@felixxm claims "Creating a single method to catch and handle all kind of 
exceptions is error prone."
But the reverse things is true: not handling exception automatically leads 
to many code errors.

@felixxm's claim as valid as "use CGI instead of Django: using a CMS is 
error-prone, specify in the code exactly what you want rather to relying to 
a single method."
If code can be simplified, it needs to be simplified.

Please support my feature request.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/5f57ace7-6079-4f10-94ef-e7c9a67c7883%40googlegroups.com.

Reply via email to