docs: def handle_api_exceptions(
      ctx, opts, context_msg=None, nl=False, exit_on_error=True, reraise_on_error=False
  ):
      """Context manager that handles API exceptions."""
      # flake8: ignore=C901
      # Use stderr for messages if the output is something else (e.g.  # JSON)
      use_stderr = opts.output != "pretty"
  
      try:
          yield
      except ApiException as exc:
          if nl:
              click.echo(err=use_stderr)
              click.secho("ERROR: ", fg="red", nl=False, err=use_stderr)
          else:
              click.secho("ERROR", fg="red", err=use_stderr)
  
          context_msg = context_msg or "Failed to perform operation!"
          click.secho(
              "%(context)s (status: %(code)s - %(code_text)s)"
              % {
                  "context": context_msg,
                  "code": exc.status,
                  "code_text": exc.status_description,
              },
              fg="red",
              err=use_stderr,
          )
  
          detail, fields = get_details(exc)
          if detail or fields:
              click.echo(err=use_stderr)
  
              if detail:
                  click.secho(
                      "Detail: %(detail)s"
                      % {"detail": click.style(detail, fg="red", bold=False)},
                      bold=True,
                      err=use_stderr,
                  )
  
              if fields:
                  for k, v in six.iteritems(fields):
                      field = "%s Field" % k.capitalize()
                      click.secho(
                          "%(field)s: %(message)s"
                          % {
                              "field": click.style(field, bold=True),
                              "message": click.style(v, fg="red"),
                          },
                          err=use_stderr,
                      )
  
          hint = get_error_hint(ctx, opts, exc)
          if hint:
              click.echo(
                  "Hint: %(hint)s" % {"hint": click.style(hint, fg="yellow")},
                  err=use_stderr,
              )
  
          if opts.verbose and not opts.debug:
              if exc.headers:
                  click.echo(err=use_stderr)
                  click.echo("Headers in Reply:", err=use_stderr)
                  for k, v in six.iteritems(exc.headers):
                      click.echo(
                          "%(key)s = %(value)s" % {"key": k, "value": v}, err=use_stderr
                      )
  
          if reraise_on_error:
              six.reraise(*sys.exc_info())
  
          if exit_on_error:
              ctx.exit(exc.status or 1)