Skip to content

Commit

Permalink
tex2pdf: proper exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
norbusan committed Sep 12, 2024
1 parent 79216d0 commit 43bff99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
11 changes: 6 additions & 5 deletions tex2pdf_service/tex2pdf/converter_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from tex2pdf.doc_converter import combine_documents, strip_to_basename
from tex2pdf.pdf_watermark import Watermark, add_watermark_text_to_pdf
from tex2pdf.service_logger import get_logger
from tex2pdf.tarball import chmod_775, unpack_tarball
from tex2pdf.tarball import ZZRMUnsupportedCompiler, ZZRMUnderspecified, chmod_775, unpack_tarball
from tex2pdf.tex_patching import fix_tex_sources
from tex2pdf.tex_to_pdf_converters import BaseConverter, select_converter_class
from tex_inspection import find_unused_toplevel_files, maybe_bbl
Expand Down Expand Up @@ -143,7 +143,7 @@ def generate_pdf(self) -> str|None:

if not self.zzrm.is_ready_for_compilation:
if not self.auto_detect:
raise Exception("Not ready for compilation and auto-detect disabled")
raise ZZRMUnderspecified("Not ready for compilation and auto-detect disabled")
logger.debug("Running preflight for input since no 00README present")
preflight_response = generate_preflight_response(self.in_dir)
self.outcome["preflight_v2"] = preflight_response.to_json()
Expand All @@ -152,14 +152,14 @@ def generate_pdf(self) -> str|None:
# TODO what to do here?
raise Exception("Preflight didn't succeed!")
if not self.zzrm.update_from_preflight(preflight_response):
raise Exception("Cannot determine compiler from preflight and sources")
raise ZZRMUnderspecified("Cannot determine compiler from preflight and sources")

# we should now be ready to go
if not self.zzrm.is_ready_for_compilation:
raise Exception("Still not ready for compilation -- this is strange")
raise ZZRMUnderspecified("Still not ready for compilation -- this is strange")

if not self.zzrm.is_supported_compiler:
raise Exception("Unsupported compiler")
raise ZZRMUnsupportedCompiler

tex_files = self.zzrm.toplevels
if self.zzrm.readme_filename is not None:
Expand Down Expand Up @@ -216,6 +216,7 @@ def generate_pdf(self) -> str|None:
def report_preflight(self) -> None:
"""Set the values to zzrm."""
if self.preflight == PreflightVersion.V1:
# should not happen, we bail out already at the API entry point.
raise ValueError("Preflight v1 is not supported anymore")
elif self.preflight == PreflightVersion.V2:
# we might have already computed preflight, don't recompute it again
Expand Down
10 changes: 10 additions & 0 deletions tex2pdf_service/tex2pdf/tarball.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class RemovedSubmission(Exception):
pass


class ZZRMUnderspecified(Exception):
"""Submitted archive either misses a 00README file or it is underspecified."""
pass


class ZZRMUnsupportedCompiler(Exception):
"""Submitted archive contains 00Readme but compiler is not supported."""
pass


def chmod_775(root_dir: str) -> None:
"""Recursively chmod 775 the directory"""
for dirpath, _dirnames, filenames in os.walk(root_dir):
Expand Down
18 changes: 16 additions & 2 deletions tex2pdf_service/tex2pdf/tex2pdf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from tex2pdf import MAX_TIME_BUDGET, USE_ADDON_TREE, MAX_TOPLEVEL_TEX_FILES, MAX_APPENDING_FILES
from tex2pdf.converter_driver import ConverterDriver, ConversionOutcomeMaker, PreflightVersion
from tex2pdf.service_logger import get_logger
from tex2pdf.tarball import save_stream, prep_tempdir, RemovedSubmission, UnsupportedArchive
from tex2pdf.tarball import ZZRMUnsupportedCompiler, ZZRMUnderspecified, save_stream, prep_tempdir, RemovedSubmission, UnsupportedArchive
from tex2pdf.fastapi_util import closer
from tex2pdf.pdf_watermark import Watermark

Expand Down Expand Up @@ -98,7 +98,7 @@ async def convert_pdf(incoming: UploadFile,
Query(title="Max Extra File count",
description=f"Maximum number of appending files. Default is {MAX_APPENDING_FILES}")] = None,
preflight: typing.Annotated[str | None,
Query(title="Preflight", description="Do preflight check, pass in version number (v1, v2)")] = None,
Query(title="Preflight", description="Do preflight check, currently only supports v2")] = None,
watermark_text: str | None = None,
watermark_link: str | None = None,
auto_detect: bool = False) -> Response:
Expand Down Expand Up @@ -128,6 +128,9 @@ async def convert_pdf(incoming: UploadFile,
preflight_version = PreflightVersion.NONE
elif preflight == "v1" or preflight == "V1":
preflight_version = PreflightVersion.V1
logger.info("Preflight version 1 not supported anymore.")
return JSONResponse(status_code=STATCODE.HTTP_400_BAD_REQUEST,
content={"message": "Preflight version 1 not supported anymore."})
elif preflight == "v2" or preflight == "V2":
preflight_version = PreflightVersion.V2
else:
Expand Down Expand Up @@ -161,10 +164,21 @@ async def convert_pdf(incoming: UploadFile,
return JSONResponse(status_code=STATCODE.HTTP_422_UNPROCESSABLE_ENTITY,
content={"message": "The source is marked deleted."})

except ZZRMUnsupportedCompiler:
logger.info("ZZRM selected compiler is not supported.")
return JSONResponse(status_code=STATCODE.HTTP_422_UNPROCESSABLE_ENTITY,
content={"message": "ZZRM selected compiler is not supported."})

except ZZRMUnderspecified:
logger.info("ZZRM missing or underspecified.")
return JSONResponse(status_code=STATCODE.HTTP_422_UNPROCESSABLE_ENTITY,
content={"message": "ZZRM missing or underspecified."})

except UnsupportedArchive:
logger.info("Archive is not supported")
return JSONResponse(status_code=STATCODE.HTTP_400_BAD_REQUEST,
content={"message": "The archive is unsupported"})

except Exception as exc:
logger.error(f"Exception %s", str(exc), exc_info=True)
return JSONResponse(status_code=STATCODE.HTTP_500_INTERNAL_SERVER_ERROR,
Expand Down

0 comments on commit 43bff99

Please sign in to comment.