Skip to content

Commit 60ec21a

Browse files
committed
feat(server, html2pdf): implement better error reporting when Chrome is not found
**WHAT:** This change improves the error message when the StrictDoc web server cannot find a Chrome installation on the system it is running on. **WHY:** Before this change, the error message was a simple Internal Server Error without an explicit indication that the Chrome could not be found. **HOW:** This integrates the upstream html2pdf4doc work where the error reporting was improved at the main.py driver program. In particular, the driver now exits with a specific exit code `COULD_NOT_FIND_CHROME = 5` that we can rely on in StrictDoc to identify the missing Chrome issue. strictdoc-project/html2pdf4doc_python#58 strictdoc-project/html2pdf4doc_python#70
1 parent 8f47295 commit 60ec21a

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ dependencies = [
9595
"WebSockets",
9696

9797
# HTML2PDF dependencies
98-
"html2pdf4doc >= 0.0.23",
98+
"html2pdf4doc >= 0.0.24",
9999

100100
# Robot Framework dependencies
101101
"robotframework >= 4.0.0",

strictdoc/export/html2pdf/pdf_print_driver.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@
33
"""
44

55
import os.path
6-
from subprocess import CompletedProcess, TimeoutExpired, run
6+
from subprocess import CalledProcessError, CompletedProcess, TimeoutExpired, run
77
from typing import List, Tuple
88

9+
from html2pdf4doc.main import HPDExitCode
10+
911
from strictdoc.core.project_config import ProjectConfig
1012
from strictdoc.helpers.timing import measure_performance
1113

1214

15+
class PDFPrintDriverException(Exception):
16+
def __init__(self, exception: Exception):
17+
super().__init__()
18+
self.exception: Exception = exception
19+
20+
def is_timeout_error(self) -> bool:
21+
return isinstance(self.exception, TimeoutExpired)
22+
23+
def is_could_not_detect_chrome(self) -> bool:
24+
return (
25+
isinstance(self.exception, CalledProcessError)
26+
and self.exception.returncode == HPDExitCode.COULD_NOT_FIND_CHROME
27+
)
28+
29+
def is_js_success_timeout(self) -> bool:
30+
return (
31+
isinstance(self.exception, CalledProcessError)
32+
and self.exception.returncode
33+
== HPDExitCode.DID_NOT_RECEIVE_SUCCESS_STATUS_FROM_HTML2PDF4DOC_JS
34+
)
35+
36+
1337
class PDFPrintDriver:
1438
@staticmethod
1539
def get_pdf_from_html(
@@ -52,7 +76,7 @@ def get_pdf_from_html(
5276
_: CompletedProcess[bytes] = run(
5377
cmd,
5478
capture_output=False,
55-
check=False,
79+
check=True,
5680
)
57-
except TimeoutExpired:
58-
raise TimeoutError from None
81+
except Exception as e_:
82+
raise PDFPrintDriverException(e_) from e_

strictdoc/server/routers/main_router.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@
113113
from strictdoc.export.html.html_templates import HTMLTemplates, JinjaEnvironment
114114
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
115115
from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer
116-
from strictdoc.export.html2pdf.pdf_print_driver import PDFPrintDriver
116+
from strictdoc.export.html2pdf.pdf_print_driver import (
117+
PDFPrintDriver,
118+
PDFPrintDriverException,
119+
)
117120
from strictdoc.export.json.json_generator import JSONGenerator
118121
from strictdoc.helpers.cast import assert_cast
119122
from strictdoc.helpers.file_modification_time import (
@@ -134,7 +137,7 @@
134137

135138
HTTP_STATUS_BAD_REQUEST = 400
136139
HTTP_STATUS_PRECONDITION_FAILED = 412
137-
HTTP_STATUS_GATEWAY_TIMEOUT = 504
140+
HTTP_STATUS_INTERNAL_SERVER_ERROR = 500
138141

139142
AUTOCOMPLETE_LIMIT = 50
140143

@@ -2158,10 +2161,25 @@ def get_export_html2pdf(document_mid: str) -> Response: # noqa: ARG001
21582161
project_config,
21592162
[(path_to_output_html, path_to_output_pdf)],
21602163
)
2161-
except TimeoutError: # pragma: no cover
2164+
except PDFPrintDriverException as e_: # pragma: no cover
2165+
if e_.is_could_not_detect_chrome():
2166+
return Response(
2167+
content="HTML2PDF could not detect an existing Chrome installation.",
2168+
status_code=HTTP_STATUS_INTERNAL_SERVER_ERROR,
2169+
)
2170+
if e_.is_timeout_error():
2171+
return Response(
2172+
content="HTML2PDF timeout error.",
2173+
status_code=HTTP_STATUS_INTERNAL_SERVER_ERROR,
2174+
)
2175+
if e_.is_js_success_timeout():
2176+
return Response(
2177+
content="HTML2PDF.js success timeout error.",
2178+
status_code=HTTP_STATUS_INTERNAL_SERVER_ERROR,
2179+
)
21622180
return Response(
2163-
content="HTML2PDF timeout error.",
2164-
status_code=HTTP_STATUS_GATEWAY_TIMEOUT,
2181+
content="HTML2PDF internal error.",
2182+
status_code=HTTP_STATUS_INTERNAL_SERVER_ERROR,
21652183
)
21662184

21672185
return FileResponse(

0 commit comments

Comments
 (0)