Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
RenderException
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 fromThrowable
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace Celema\Boiler\Exception;
6
7use Celema\Boiler\Location;
8use Throwable;
9
10final class RenderException extends RuntimeException implements TemplateException
11{
12    public static function fromThrowable(string $path, Throwable $throwable): self
13    {
14        $location =
15            $throwable instanceof RuntimeException || $throwable instanceof LogicException
16                ? $throwable->location()
17                : null;
18        $location ??= Location::fromThrowable($path, $throwable);
19        $message = $location->line === null
20            ? "Template rendering error ({$path})"
21            : "Template rendering error at {$location}";
22
23        // Keep the original code so handlers that read it, e.g. for an HTTP
24        // status, still can; non-integer codes such as PDO's SQLSTATE cannot
25        // be passed on.
26        $code = $throwable->getCode();
27
28        return new self(
29            $message . ': ' . $throwable->getMessage(),
30            is_int($code) ? $code : 0,
31            $throwable,
32            $location,
33        );
34    }
35}