Error handling

Catch them all!

Typeset.sh attempts to render your document even when errors occur, such as incorrect use of CSS properties or unsupported features. These errors are caught and stored within the result object for further handling, e.g. logging.

Similar, the standard UriResolver also catches any exceptions, such as file not found or access denied, and stores them in the UriResolver instance.

However, some errors may still prevent the document from rendering, in which case a try-catch block can be utilized. The example provided demonstrates how to dump all errors as additional headers.

try {
    $html = <<<HTML
        <p>Hello,</p>
        <p>This is an simple example.</p>
    HTML;
    
    $resolveUri = \Typesetsh\UriResolver::all();
    $result = \Typesetsh\createPdf($html, $resolveUri);

    $data = $result->asString();
    header('Content-Type: application/pdf');
    header('Content-Length: ' . strlen($data));
    header("Content-Disposition:inline;filename=hello.pdf");
    
    /* Merge PDF errors and resolver errors */
    foreach ([...$result->issues, ...$resolveUri->errors] as $issue) {
        header("X-PDF-Warning: ".$issue->getMessage());
    }

    echo $data;

} catch (Exception $exception) {
    // Snap!
    http_response_code(500);
    echo "Error!";
}

Last updated