code: def handle_error(index_name, keep=False):
      '''
      Handle errors while indexing.
      In case of error, properly log it, remove the index and exit.
      If `keep` is `True`, index is not deleted.
      '''
      # Handle keyboard interrupt
      signal.signal(signal.SIGINT, signal.default_int_handler)
      signal.signal(signal.SIGTERM, signal.default_int_handler)
      has_error = False
      try:
          yield
      except KeyboardInterrupt:
          print('')  # Proper warning message under the "^C" display
          log.warning('Interrupted by signal')
          has_error = True
      except Exception as e:
          log.error(e)
          has_error = True
      if has_error:
          if not keep:
              log.info('Removing index %s', index_name)
              es.indices.delete(index=index_name)
          sys.exit(-1f handle_error(index_name, keep=False):
      '''
      Handle errors while indexing.
      In case of error, properly log it, remove the index and exit.
      If `keep` is `True`, index is not deleted.
      '''
      # Handle keyboard interrupt
      signal.signal(signal.SIGINT, signal.default_int_handler)
      signal.signal(signal.SIGTERM, signal.default_int_handler)
      has_error = False
      try:
          yield
      except KeyboardInterrupt:
          print('')  # Proper warning message under the "^C" display
          log.warning('Interrupted by signal')
          has_error = True
      except Exception as e:
          log.error(e)
          has_error = True
      if has_error:
          if not keep:
              log.info('Removing index %s', index_name)
              es.indices.delete(index=index_name)
          sys.exit(-1)
docs: Handle errors while indexing.
      In case of error, properly log it, remove the index and exit.
      If `keep` is `True`, index is not deleted.