code: def cli_exception_handler(
      fn: Callable[
          [argparse.Namespace, OcrmypdfPluginManager], ExitCode
      ],
      options: argparse.Namespace,
      plugin_manager: OcrmypdfPluginManager,
  ) -> ExitCode:
      """Convert exceptions into command line error messages and exit codes.
  
      When known exceptions are raised, the exception message is printed to stderr
      and the program exits with a non-zero exit code. When unknown exceptions are
      raised, the exception traceback is printed to stderr and the program exits
      with a non-zero exit code.
      """
      try:
          # We cannot use a generator and yield here, as would be the usual pattern
          # for exception handling context managers, because we need to return an exit
          # code.
          return fn(options, plugin_manager)
      except KeyboardInterrupt:
          if options.verbose >= 1:
              log.exception("KeyboardInterrupt")
          else:
              log.error("KeyboardInterrupt")
          return ExitCode.ctrl_c
      except ExitCodeException as e:
          e = cast(ExitCodeException, e)
          if options.verbose >= 1:
              log.exception("ExitCodeException")
          elif str(e):
              log.error("%s: %s", type(e).__name__, str(e))
          else:
              log.error(type(e).__name__)
          return e.exit_code
      except PIL.Image.DecompressionBombError:
          log.exception(
              "A decompression bomb error was encountered while executing the "
              "pipeline. Use the argument --max-image-mpixels to raise the maximum "
              "image pixel limit."
          )
          return ExitCode.other_error
      except (
          BrokenProcessPool,
          BrokenThreadPool,
      ):
          log.exception(
              "A worker process was terminated unexpectedly. This is known to occur if "
              "processing your file takes all available swap space and RAM. It may "
              "help to try again with a smaller number of jobs, using the --jobs "
              "argument."
          )
          return ExitCode.child_process_error
      except Exception:  # pylint: disable=broad-except
          log.exception(
              "An exception occurred while executing the pipeline"
          )
          return ExitCode.other_error
  
imports: from __future__ import annotations
  import argparse
  import json
  import logging
  import logging.handlers
  import os
  import shutil
  import sys
  import threading
  from collections.abc import Sequence
  from concurrent.futures.process import BrokenProcessPool
  from concurrent.futures.thread import BrokenThreadPool
  from contextlib import contextmanager
  from dataclasses import dataclass
  from pathlib import Path
  from typing import Callable, NamedTuple, cast
  import PIL
  from ocrmypdf._concurrent import Executor, setup_executor
  from ocrmypdf._jobcontext import PageContext, PdfContext
  from ocrmypdf._logging import PageNumberFilter
  from ocrmypdf._metadata import metadata_fixup
  from ocrmypdf._pipeline import convert_to_pdfa, create_ocr_image, create_pdf_page_from_image, create_visible_page_jpg, generate_postscript_stub, get_orientation_correction, get_pdf_save_settings, optimize_pdf, preprocess_clean, preprocess_deskew, preprocess_remove_background, rasterize, rasterize_preview, should_linearize, should_visible_page_image_use_jpg
  from ocrmypdf._plugin_manager import OcrmypdfPluginManager
  from ocrmypdf._validation import report_output_file_size
  from ocrmypdf.exceptions import ExitCode, ExitCodeException
  from ocrmypdf.helpers import available_cpu_count, check_pdf, pikepdf_enable_mmap, samefile
  from ocrmypdf.pdfa import file_claims_pdfa
label_desc: Use image node_id 7 for calling a global function with example usage: cli_exception_handler(fn, options, plugin_manager) and returns: fn, ExitCode, e, ExitCode, ExitCode, ExitCode