code: def handle_exception(
      app: Sphinx | None,
      args: Any,
      exception: BaseException,
      stderr: TextIO = sys.stderr,
  ) -> None:
      if isinstance(exception, bdb.BdbQuit):
          return
  
      if args.pdb:
          print(
              red(
                  __(
                      "Exception occurred while building, starting debugger:"
                  )
              ),
              file=stderr,
          )
          traceback.print_exc()
          pdb.post_mortem(sys.exc_info()[2])
      else:
          print(file=stderr)
          if args.verbosity or args.traceback:
              exc = sys.exc_info()[1]
              if isinstance(exc, SphinxParallelError):
                  exc_format = (
                      "(Error in parallel process)\n" + exc.traceback
                  )
                  print(exc_format, file=stderr)
              else:
                  traceback.print_exc(None, stderr)
                  print(file=stderr)
          if isinstance(exception, KeyboardInterrupt):
              print(__("Interrupted!"), file=stderr)
          elif isinstance(exception, SystemMessage):
              print(red(__("reST markup error:")), file=stderr)
              print(terminal_safe(exception.args[0]), file=stderr)
          elif isinstance(exception, SphinxError):
              print(red("%s:" % exception.category), file=stderr)
              print(str(exception), file=stderr)
          elif isinstance(exception, UnicodeError):
              print(red(__("Encoding error:")), file=stderr)
              print(terminal_safe(str(exception)), file=stderr)
              tbpath = save_traceback(app, exception)
              print(
                  red(
                      __(
                          "The full traceback has been saved in %s, if you want "
                          "to report the issue to the developers."
                      )
                      % tbpath
                  ),
                  file=stderr,
              )
          elif isinstance(
              exception, RuntimeError
          ) and "recursion depth" in str(exception):
              print(red(__("Recursion error:")), file=stderr)
              print(terminal_safe(str(exception)), file=stderr)
              print(file=stderr)
              print(
                  __(
                      "This can happen with very large or deeply nested source "
                      "files. You can carefully increase the default Python "
                      "recursion limit of 1000 in conf.py with e.g.:"
                  ),
                  file=stderr,
              )
              print(
                  "    import sys; sys.setrecursionlimit(1500)",
                  file=stderr,
              )
          else:
              print(red(__("Exception occurred:")), file=stderr)
              print(format_exception_cut_frames().rstrip(), file=stderr)
              tbpath = save_traceback(app, exception)
              print(
                  red(
                      __(
                          "The full traceback has been saved in %s, if you "
                          "want to report the issue to the developers."
                      )
                      % tbpath
                  ),
                  file=stderr,
              )
              print(
                  __(
                      "Please also report this if it was a user error, so "
                      "that a better error message can be provided next time."
                  ),
                  file=stderr,
              )
              print(
                  __(
                      "A bug report can be filed in the tracker at "
                      "<https://github.com/sphinx-doc/sphinx/issues>. Thanks!"
                  ),
                  file=stderr,
              )
  
imports: from __future__ import annotations
  import argparse
  import bdb
  import contextlib
  import locale
  import multiprocessing
  import os
  import pdb
  import sys
  import traceback
  from os import path
  from typing import TYPE_CHECKING, Any, TextIO
  from docutils.utils import SystemMessage
  import sphinx.locale
  from sphinx import __display_version__
  from sphinx.application import Sphinx
  from sphinx.errors import SphinxError, SphinxParallelError
  from sphinx.locale import __
  from sphinx.util._io import TeeStripANSI
  from sphinx.util.console import color_terminal, nocolor, red, terminal_safe
  from sphinx.util.docutils import docutils_namespace, patch_docutils
  from sphinx.util.exceptions import format_exception_cut_frames, save_traceback
  from sphinx.util.osutil import ensuredir
label_desc: Use image node_id 1 for calling a global function with example usage: handle_exception(app, args, exception, stderr) without return types