2012-01-10 14 views
8

मैं किसी त्रुटि कोड के साथ हेडर लौटने की बजाय कुछ JSON प्रतिक्रियाएं वापस लौटना चाहूंगा। क्या इस तरह की त्रुटियों को संभालने के लिए tastypie में कोई रास्ता है?Tastypie-django कस्टम त्रुटि हैंडलिंग

+0

अंत में इसे समझ लिया। अगर किसी और को इसकी ज़रूरत है, तो देखने के लिए यहां एक अच्छा संसाधन है। https://gist.github.com/1116962 –

+0

यदि आप कर सकते हैं, इसे साफ़ करें और समुदाय की सहायता के लिए इसे उत्तर दें – Dave

उत्तर

5

अंत में यह पता चला। अगर किसी और को इसकी ज़रूरत है, तो देखने के लिए यहां एक अच्छा संसाधन है। http://gist.github.com/1116962

class YourResource(ModelResource): 

    def wrap_view(self, view): 
     """ 
     Wraps views to return custom error codes instead of generic 500's 
     """ 
     @csrf_exempt 
     def wrapper(request, *args, **kwargs): 
      try: 
       callback = getattr(self, view) 
       response = callback(request, *args, **kwargs) 

       if request.is_ajax(): 
        patch_cache_control(response, no_cache=True) 

       # response is a HttpResponse object, so follow Django's instructions 
       # to change it to your needs before you return it. 
       # https://docs.djangoproject.com/en/dev/ref/request-response/ 
       return response 
      except (BadRequest, ApiFieldError), e: 
       return HttpBadRequest({'code': 666, 'message':e.args[0]}) 
      except ValidationError, e: 
       # Or do some JSON wrapping around the standard 500 
       return HttpBadRequest({'code': 777, 'message':', '.join(e.messages)}) 
      except Exception, e: 
       # Rather than re-raising, we're going to things similar to 
       # what Django does. The difference is returning a serialized 
       # error message. 
       return self._handle_500(request, e) 

     return wrapper 
3

आप tastypie के Resource विधि _handle_500() के ऊपर लिख सकता है। तथ्य यह है कि यह अंडरस्कोर से शुरू होता है, वास्तव में इंगित करता है कि यह एक "निजी" विधि है और इसे ओवरराइट नहीं किया जाना चाहिए, लेकिन मुझे इसे wrap_view() को ओवरराइट करने और बहुत सारे तर्क को दोहराने के बजाय एक क्लीनर तरीका मिल गया है।

from tastypie import http 
from tastypie.resources import ModelResource 
from tastypie.exceptions import TastypieError 

class MyResource(ModelResource): 

    class Meta: 
     queryset = MyModel.objects.all() 
     fields = ('my', 'fields') 

    def _handle_500(self, request, exception): 

     if isinstance(exception, TastypieError): 

      data = { 
       'error_message': getattr(
        settings, 
        'TASTYPIE_CANNED_ERROR', 
        'Sorry, this request could not be processed.' 
       ), 
      } 

      return self.error_response(
       request, 
       data, 
       response_class=http.HttpApplicationError 
      ) 

     else: 
      return super(MyResource, self)._handle_500(request, exception) 

इस मामले में मैं यह पता चल सके exceptionTastypieError का एक उदाहरण है और संदेश के साथ एक JSON प्रतिक्रिया वापसी "क्षमा करें, इस अनुरोध कर सकता द्वारा सभी Tastypie त्रुटियां पकड़:

इस तरह से मैं इसका इस्तेमाल है संसाधित नहीं किया जा सकता है। " यदि यह एक अलग अपवाद है, तो मैं super() का उपयोग कर पैरेंट _handle_500 पर कॉल करता हूं, जो विकास मोड में django त्रुटि पृष्ठ या send_admins() उत्पादन मोड में बना देगा।

यदि आप किसी विशिष्ट अपवाद के लिए विशिष्ट JSON प्रतिक्रिया चाहते हैं, तो बस isinstance() एक विशिष्ट अपवाद पर जांचें। यहाँ सब Tastypie अपवाद हैं:

https://github.com/toastdriven/django-tastypie/blob/master/tastypie/exceptions.py

वास्तव में मुझे लगता है कि एक बेहतर/क्लीनर Tastypie में यह करने के लिए जिस तरह से होना चाहिए, इसलिए मैं उनके GitHub पर opened a ticket

संबंधित मुद्दे