Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ composer.lock
/phpunit.phar
/phpunit.xml
/.phpunit.cache
.phpunit.result.cache

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"source-directory": "config"
},
"config-plugin": {
"di-web": "di-web.php"
"di-web": "di-web.php",
"params": "params.php"
}
},
"config": {
Expand Down
5 changes: 5 additions & 0 deletions config/di-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@

return [
ThrowableRendererInterface::class => HtmlRenderer::class,
HtmlRenderer::class => [
'__construct()' => [
'solutionProviders' => $params['yiisoft/error-handler']['solutionProviders'],
],
],
ThrowableResponseFactoryInterface::class => ThrowableResponseFactory::class,
];
16 changes: 16 additions & 0 deletions config/params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);


/**
* @var array $params
*/

return [
'yiisoft/error-handler' => [
'solutionProviders' => [
Yiisoft\Definitions\Reference::to(Yiisoft\ErrorHandler\Solution\FriendlyExceptionSolution::class),
],
],
];
7 changes: 7 additions & 0 deletions src/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ final class HtmlRenderer implements ThrowableRendererInterface
* maxTraceLines?: int,
* traceHeaderLine?: string,
* } $settings
* @param \Yiisoft\ErrorHandler\Solution\SolutionProviderInterface[] $solutionProviders
*/
public function __construct(
array $settings = [],
Expand All @@ -137,11 +138,13 @@ public function __construct(
?int $maxSourceLines = null,
?int $maxTraceLines = null,
?string $traceHeaderLine = null,
private readonly array $solutionProviders = []
) {
$this->markdownParser = new GithubMarkdown();
$this->markdownParser->html5 = true;

$this->defaultTemplatePath = dirname(__DIR__, 2) . '/templates';

$this->template = $template
?? $settings['template']
?? $this->defaultTemplatePath . '/production.php';
Expand Down Expand Up @@ -176,6 +179,7 @@ public function renderVerbose(Throwable $t, ?ServerRequestInterface $request = n
$this->renderTemplate($this->verboseTemplate, [
'request' => $request,
'throwable' => $t,
'solutions' => $this->solutionProviders,
]),
[Header::CONTENT_TYPE => self::CONTENT_TYPE],
);
Expand Down Expand Up @@ -233,6 +237,9 @@ public function parseMarkdown(string $content): string
'ol',
'li',
'img',
'form',
'input',
'button',
]);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Solution/FriendlyExceptionSolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ErrorHandler\Solution;

use Yiisoft\FriendlyException\FriendlyExceptionInterface;

final class FriendlyExceptionSolution implements SolutionProviderInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class FriendlyExceptionSolution implements SolutionProviderInterface
/**
* Renders a friendly exception.
*/
final class FriendlyExceptionSolution implements SolutionProviderInterface

{
public function supports(\Throwable $e): bool
{
return $e instanceof FriendlyExceptionInterface && $e->getSolution() !== null;
}

public function generate(\Throwable $e): string
{
return $e->getSolution();

Check failure on line 18 in src/Solution/FriendlyExceptionSolution.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

UndefinedInterfaceMethod

src/Solution/FriendlyExceptionSolution.php:18:20: UndefinedInterfaceMethod: Method Throwable::getSolution does not exist (see https://psalm.dev/181)

Check failure on line 18 in src/Solution/FriendlyExceptionSolution.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

MixedReturnStatement

src/Solution/FriendlyExceptionSolution.php:18:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
}
}
22 changes: 22 additions & 0 deletions src/Solution/SolutionProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ErrorHandler\Solution;

/**
* The interface declares an adapter to render a solution for an event.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The interface declares an adapter to render a solution for an event.
* Solution provider finds and renders a solution for a given throwable.

* Basically, it renders the error message as-is, but possible could render a button with click-to-fix action that will be handled by an HTTP request (with middleware) back to server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Basically, it renders the error message as-is, but possible could render a button with click-to-fix action that will be handled by an HTTP request (with middleware) back to server.
* It may render the message as-is, but it could also provide a button with a click-to-fix action that will be handled by an async HTTP request (using a separate middleware).

*/
interface SolutionProviderInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SolutionProviderInterface must be placed into separate package. yiisoft/error-handler is too heavy dependency for packages, that implement SolutionProviderInterface.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about any solution in the air. It's about the specific contract between web, user, application and error.
It shouldn't be separated. At least for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface should define format of solution. Users of interface should know about format of solution.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML for sure

{
/**
* Returns true if the implementation may suggest more than regular provider.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns true if the implementation may suggest more than regular provider.
* Returns true if solution could be provided for a given throwable.

*/
public function supports(\Throwable $e): bool;

/**
* Generates an HTML code with solution which will be clean by {@see \Yiisoft\ErrorHandler\Renderer\HtmlRenderer} and shown to the end user.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "clean by"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HtmlRenderer has HTML purifier inside, it cleans all the given html tags and attributes except that are in configuration

*/
public function generate(\Throwable $e): string;
}
18 changes: 14 additions & 4 deletions templates/development.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
use Yiisoft\ErrorHandler\CompositeException;
use Yiisoft\ErrorHandler\Exception\ErrorException;
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
use Yiisoft\ErrorHandler\Solution\SolutionProviderInterface;
use Yiisoft\FriendlyException\FriendlyExceptionInterface;

/**
* @var $this HtmlRenderer
* @var $request ServerRequestInterface|null
* @var $throwable Throwable
* @var $solutions SolutionProviderInterface[]
*/

$theme = $_COOKIE['yii-exception-theme'] ?? '';
Expand All @@ -19,7 +21,6 @@
$throwable = $throwable->getFirstException();
}
$isFriendlyException = $throwable instanceof FriendlyExceptionInterface;
$solution = $isFriendlyException ? $throwable->getSolution() : null;
$exceptionClass = get_class($throwable);
$exceptionMessage = $throwable->getMessage();

Expand Down Expand Up @@ -92,9 +93,18 @@
<?= nl2br($this->htmlEncode($exceptionMessage)) ?>
</div>

<?php if ($solution !== null): ?>
<div class="solution"><?= $this->parseMarkdown($solution) ?></div>
<?php endif ?>
<?php
if (!empty($solutions)) {
echo '<div class="solutions">';
foreach ($solutions as $i => $solution) {
if ($solution->supports($throwable)) {
echo '<div class="solution solution-' . $i . '">';
echo $this->parseMarkdown($solution->generate($throwable));
echo '</div>';
}
}
}
?>

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

Expand Down
3 changes: 3 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ private function createContainer(?string $postfix = null): Container

private function getDiConfig(?string $postfix = null): array
{
$params = [
'yiisoft/error-handler' => ['solutionProviders' => []],
];
return require dirname(__DIR__) . '/config/di' . ($postfix !== null ? '-' . $postfix : '') . '.php';
}
}
Loading