'Wraps methods so they can be called in a more functional way as well
as handling exceptions better.
Note that if ``BadRequest`` or an exception with a ``response`` attr
are seen, there is special handling to either present a message back
to the user or return the response traveling with the exception.'

def wrap_view(self, view):

    @csrf_exempt
    def wrapper(request, *args, **kwargs):
        try:
            callback = getattr(self, view)
            response = callback(request, *args, **kwargs)
            varies = getattr(self._meta.cache, u'varies', [])
            if varies:
                patch_vary_headers(response, varies)
            if self._meta.cache.cacheable(request, response):
                if self._meta.cache.cache_control():
                    patch_cache_control(response, **self._meta.cache.cache_control())
            if (request.is_ajax() and (not response.has_header(u'Cache-Control'))):
                patch_cache_control(response, no_cache=True)
            return response
        except (BadRequest, fields.ApiFieldError) as e:
            data = {u'error': (sanitize(e.args[0]) if getattr(e, u'args') else u'')}
            return self.error_response(request, data, response_class=http.HttpBadRequest)
        except ValidationError as e:
            data = {u'error': sanitize(e.messages)}
            return self.error_response(request, data, response_class=http.HttpBadRequest)
        except Exception as e:
            if (hasattr(e, u'response') and isinstance(e.response, HttpResponse)):
                return e.response
            if (settings.DEBUG and getattr(settings, u'TASTYPIE_FULL_DEBUG', False)):
                raise
            if (request.META.get(u'SERVER_NAME') == u'testserver'):
                raise
            return self._handle_500(request, e)
    return wrapper
