import threading
import pprint


def handle_threading_error(scans_completed, threading_error):
    """
    Catch threading errors such as "error: can't start new thread"
    and handle them in a specific way
    """
    active_threads = threading.active_count()
    
    def nice_thread_repr(alive_threads):
        repr_alive = [repr(x) for x in alive_threads]
        repr_alive.sort()
        return pprint.pformat(repr_alive)
    
    pprint_threads = nice_thread_repr(threading.enumerate())
    
    msg = ('A "%s" threading error was found.\n'
           ' The current process has a total of %s active threads and has'
           ' completed %s scans. The complete list of threads follows:\n\n%s')
    raise Exception(msg % (threading_error, active_threads,
                           scans_completed, pprint_threads))