1. Go to this page and download the library: Download phpdot/error-handler library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpdot / error-handler example snippets
use PHPdot\ErrorHandler\ErrorHandler;
ErrorHandler::register('development');
use PHPdot\ErrorHandler\Middleware\ErrorHandlerMiddleware;
$middleware = new ErrorHandlerMiddleware(
handler: $exceptionHandler,
responseFactory: $responseFactory, // PSR-17
streamFactory: $streamFactory, // PSR-17
);
$app->pipe($middleware);
use PHPdot\ErrorHandler\ExceptionHandler;
use PHPdot\ErrorHandler\Renderer\HtmlDevRenderer;
use PHPdot\ErrorHandler\Renderer\HtmlProdRenderer;
use PHPdot\ErrorHandler\Renderer\JsonRenderer;
$handler = new ExceptionHandler(
environment: 'development',
devRenderer: new HtmlDevRenderer(),
prodRenderer: new HtmlProdRenderer(),
jsonRenderer: new JsonRenderer(),
);
$body = $handler->handle($exception, $request);
use PHPdot\ErrorHandler\Contract\SolutionProviderInterface;
use PHPdot\ErrorHandler\Solution\Solution;
use PHPdot\ErrorHandler\Solution\SolutionLink;
final class ClassNotFoundSolution implements SolutionProviderInterface
{
public function canSolve(\Throwable $exception): bool
{
return $exception instanceof \Error
&& str_contains($exception->getMessage(), 'not found');
}
public function getSolutions(\Throwable $exception): array
{
return [
new Solution(
title: 'Class not found',
description: "Check the namespace and run 'composer dump-autoload'.",
links: [
new SolutionLink('Composer Autoloading', 'https://getcomposer.org/doc/01-basic-usage.md#autoloading'),
],
),
];
}
}
ErrorHandler::register('development')
->addSolutionProvider(new ClassNotFoundSolution());
use PHPdot\ErrorHandler\Contract\ContextProviderInterface;
use Psr\Http\Message\ServerRequestInterface;
final class RouteContextProvider implements ContextProviderInterface
{
public function getLabel(): string
{
return 'Route';
}
public function collect(\Throwable $exception, ?ServerRequestInterface $request): array
{
return [
'method' => $request?->getMethod() ?? 'N/A',
'path' => $request?->getUri()->getPath() ?? 'N/A',
];
}
}