PHP code example of phpdot / error-handler

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');

ErrorHandler::register('production')
    ->setLogger($logger);

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',
        ];
    }
}
mermaid
graph TD
    THROWABLE["Throwable<br/><br/>uncaught exception, converted PHP error,<br/>or fatal caught on shutdown"]
    ENTRY["Entry point<br/><br/>ErrorHandler (global handlers)<br/>or ErrorHandlerMiddleware (PSR-15)"]
    HANDLER["ExceptionHandler<br/><br/>maps status code, logs via PSR-3,<br/>selects the renderer"]
    CONTEXT["ErrorContext<br/><br/>StackTrace + Frames + CodeLines,<br/>filtered environment, tabs, solutions"]
    PROVIDERS["Providers<br/><br/>ContextProviderInterface (tabs)<br/>SolutionProviderInterface (fixes)"]
    RENDER["RendererInterface<br/><br/>HtmlDev / HtmlProd / Json (RFC 9457) / PlainText"]
    OUT["Rendered output<br/><br/>string body, or PSR-7 response<br/>from the middleware"]

    THROWABLE --> ENTRY
    ENTRY --> HANDLER
    HANDLER --> CONTEXT
    PROVIDERS --> CONTEXT
    CONTEXT --> RENDER
    RENDER --> OUT