def handle_es_errors(template, status_code=503):
 def handler(fun):
  @wraps(fun)
  def _handler(request, *args, **kwargs):
   try:
    return fun(request, *args, **kwargs)
   except ES_EXCEPTIONS as exc:
    is_json = (request.GET.get('format') == 'json')
    callback = request.GET.get('callback', '').strip()
    content_type = ('application/x-javascript' if callback else 'application/json')
    if is_json:
     return HttpResponse(json.dumps({'error': _('Search Unavailable')}), content_type=content_type, status=status_code)
    if callable(template):
     actual_template = template(request, *args, **kwargs)
    else:
     actual_template = template
    log.exception(exc)
    return render(request, actual_template, status=503)
  return _handler
 return handler

def handle_es_errors ( template , status_code = 503 ) : def handler ( fun ) : @ wraps ( fun ) def _handler ( request , * args , ** kwargs ) : try : return fun ( request , * args , ** kwargs ) except ES_EXCEPTIONS as exc : is_json = ( request . GET . get ( 'format' ) == 'json' ) callback = request . GET . get ( 'callback' , '' ) . strip ( ) content_type = ( 'application/x-javascript' if callback else 'application/json' ) if is_json : return HttpResponse ( json . dumps ( { 'error' : _ ( 'Search Unavailable' ) } ) , content_type = content_type , status = status_code ) if callable ( template ) : actual_template = template ( request , * args , ** kwargs ) else : actual_template = template log . exception ( exc ) return render ( request , actual_template , status = 503 ) return _handler return handler
handles elasticsearch exceptions for views wrap the entire view in this and dont worry about elasticsearch exceptions again! :arg template: template path string or function to generate the template path string for html requests :arg status_code: status code to return :returns: content-type-appropriate httpresponse .