Skip to content

Commit f09026b

Browse files
committed
Add solution providers
1 parent ae71fd1 commit f09026b

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

src/Renderer/HtmlRenderer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Yiisoft\ErrorHandler\CompositeException;
1313
use Yiisoft\ErrorHandler\ErrorData;
1414
use Yiisoft\ErrorHandler\Exception\ErrorException;
15+
use Yiisoft\ErrorHandler\Solution\SolutionGenerator;
16+
use Yiisoft\ErrorHandler\Solution\FriendlyExceptionSolution;
1517
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
1618
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
1719

@@ -104,6 +106,8 @@ final class HtmlRenderer implements ThrowableRendererInterface
104106
*/
105107
private ?array $vendorPaths = null;
106108

109+
private SolutionGenerator $solutionGenerator;
110+
107111
/**
108112
* @param array $settings Settings can have the following keys:
109113
* - template: string, full path of the template file for rendering exceptions without call stack information.
@@ -131,6 +135,7 @@ public function __construct(array $settings = [])
131135
$this->maxSourceLines = $settings['maxSourceLines'] ?? 19;
132136
$this->maxTraceLines = $settings['maxTraceLines'] ?? 13;
133137
$this->traceHeaderLine = $settings['traceHeaderLine'] ?? null;
138+
$this->solutionGenerator = new SolutionGenerator([new FriendlyExceptionSolution()]);
134139
}
135140

136141
public function render(Throwable $t, ServerRequestInterface $request = null): ErrorData
@@ -143,9 +148,12 @@ public function render(Throwable $t, ServerRequestInterface $request = null): Er
143148

144149
public function renderVerbose(Throwable $t, ServerRequestInterface $request = null): ErrorData
145150
{
151+
$solutions = $this->solutionGenerator->generate($t);
152+
146153
return new ErrorData($this->renderTemplate($this->verboseTemplate, [
147154
'request' => $request,
148155
'throwable' => $t,
156+
'solutions' => $solutions,
149157
]));
150158
}
151159

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\ErrorHandler\Solution;
6+
7+
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
8+
9+
final class FriendlyExceptionSolution implements SolutionProviderInterface
10+
{
11+
public function supports(\Throwable $e): bool
12+
{
13+
return $e instanceof FriendlyExceptionInterface && $e->getSolution() !== null;
14+
}
15+
16+
public function generate(\Throwable $e): string
17+
{
18+
return $e->getSolution();
19+
}
20+
}

src/Solution/SolutionGenerator.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\ErrorHandler\Solution;
6+
7+
final class SolutionGenerator
8+
{
9+
public function __construct(
10+
/**
11+
* @var SolutionGeneratorInterface[]
12+
*/
13+
private array $generators,
14+
) {
15+
}
16+
17+
/**
18+
* @return string[]
19+
*/
20+
public function generate(\Throwable $e): array
21+
{
22+
$result = [];
23+
24+
foreach ($this->generators as $generator) {
25+
if ($generator->supports($e)) {
26+
$result[] = $generator->generate($e);
27+
}
28+
}
29+
return $result;
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\ErrorHandler\Solution;
6+
7+
interface SolutionProviderInterface
8+
{
9+
public function supports(\Throwable $e): bool;
10+
11+
public function generate(\Throwable $e): string;
12+
}

templates/development.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @var $this \Yiisoft\ErrorHandler\Renderer\HtmlRenderer
99
* @var $request \Psr\Http\Message\ServerRequestInterface|null
1010
* @var $throwable \Throwable
11+
* @var $solutions string[]
1112
*/
1213

1314
$theme = $_COOKIE['yii-exception-theme'] ?? '';
@@ -17,7 +18,6 @@
1718
$throwable = $throwable->getFirstException();
1819
}
1920
$isFriendlyException = $throwable instanceof FriendlyExceptionInterface;
20-
$solution = $isFriendlyException ? $throwable->getSolution() : null;
2121
$exceptionClass = get_class($throwable);
2222
$exceptionMessage = $throwable->getMessage();
2323

@@ -79,9 +79,16 @@
7979
<?= nl2br($this->htmlEncode($exceptionMessage)) ?>
8080
</div>
8181

82-
<?php if ($solution !== null): ?>
83-
<div class="solution"><?= $this->parseMarkdown($solution) ?></div>
84-
<?php endif ?>
82+
<?php
83+
if (!empty($solutions)) {
84+
echo '<div class="solutions">';
85+
foreach ($solutions as $i => $solution) {
86+
echo '<div class="solution solution-' . $i . '">';
87+
echo $this->parseMarkdown($solution);
88+
echo '</div>';
89+
}
90+
}
91+
?>
8592

8693
<?= $this->renderPreviousExceptions($originalException) ?>
8794

0 commit comments

Comments
 (0)