From a139e64c679977c479d16a4dfe4ce05023b7243c Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 22 May 2019 18:30:30 -0700 Subject: [PATCH] api: short-circuit exception handler, as caller should provide their own --- src/ocrmypdf/_sync.py | 13 +++++++++++-- src/ocrmypdf/api.py | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ocrmypdf/_sync.py b/src/ocrmypdf/_sync.py index 5c509d22..a1a3f083 100644 --- a/src/ocrmypdf/_sync.py +++ b/src/ocrmypdf/_sync.py @@ -239,7 +239,7 @@ def exec_concurrent(context): copy_final(pdf, context.options.output_file, context) -def run_pipeline(options): +def run_pipeline(options, api=False): log = make_logger(options, __name__) # Any changes to options will not take effect for options that are already @@ -277,12 +277,21 @@ def run_pipeline(options): # Execute the pipeline exec_concurrent(context) except KeyboardInterrupt as e: + if api: + raise log.error("KeyboardInterrupt") return ExitCode.ctrl_c except ExitCodeException as e: - log.error("%s: %s" % (type(e).__name__, str(e))) + if api: + raise + if str(e): + log.error("%s: %s", type(e).__name__, str(e)) + else: + log.error(type(e).__name__) return e.exit_code except Exception as e: + if api: + raise log.exception("An exception occurred while executing the pipeline") return ExitCode.other_error diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index fc22b20b..2d6ccecf 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -179,4 +179,4 @@ def ocrmypdf( # pylint: disable=unused-argument ): options = create_options(**locals()) check_options(options) - return run_pipeline(options) + return run_pipeline(options, api=True)