xref: /plugin/totext/Exception/ExtractionException.php (revision 0fbee1892eb8b7339ce48e27e48e3fc821a8695f)
1<?php
2
3namespace dokuwiki\plugin\totext\Exception;
4
5/**
6 * Thrown when text extraction fails due to an I/O or parse error.
7 */
8class ExtractionException extends \RuntimeException
9{
10    /**
11     * Normalise a caught error to an ExtractionException.
12     *
13     * An error that is already an ExtractionException is returned unchanged so
14     * its precise message survives; anything else is wrapped with the given
15     * context and chained as the previous exception.
16     *
17     * @param \Throwable $previous the caught error
18     * @param string $context context for the failure, e.g. "Failed to open /x.pdf"
19     * @return self
20     */
21    public static function wrap(\Throwable $previous, string $context): self
22    {
23        if ($previous instanceof self) {
24            return $previous;
25        }
26        return new self($context . ': ' . $previous->getMessage(), 0, $previous);
27    }
28}
29