PHP code example of chubbyphp / chubbyphp-error-handler

1. Go to this page and download the library: Download chubbyphp/chubbyphp-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/ */

    

chubbyphp / chubbyphp-error-handler example snippets




namespace MyProject\ErrorHandler;

use Chubbyphp\ErrorHandler\ErrorHandlerProvider;

class JsonErrorResponseProvider implements ErrorHandlerProvider
{
    /**
     * @return string
     */
    public function getContentType(): string
    {
        return 'application/json';
    }

    /**
     * @param Request $request
     * @param Response $response
     * @param \Exception $exception
     * @return Response
     */
    public function get(Request $request, Response $response, \Exception $exception): Response
    {
        $response->getBody()->write(json_encode([
            'exception' => ['message' => $exception->getMessage(), 'code' => $exception->getCode()]])
        );

        return $response;
    }
}



use Chubbyphp\ErrorHandler\ErrorHandlerMiddleware;
use Chubbyphp\ErrorHandler\ErrorHandlerInterface;

$middleware = new ErrorHandlerMiddleware(new <ErrorHandlerInterface>);
$middleware($request, $response, $next);



use Chubbyphp\ErrorHandler\SimpleErrorHandler;

$errorHandler = new SimpleErrorHandler($provider);

$response = $errorHandler($request, $response, $exception);



use Chubbyphp\ErrorHandler\SimpleErrorHandlerProvider;
use MyProject\ErrorHandler\JsonErrorResponseProvider;
use Pimple/Container;

$container = new Container();
$container->register(new SimpleErrorHandlerProvider);

// IMPORTANT: without this definition, the error handler will not work!
$container['errorHandler.defaultProvider'] = function () use ($container) {
    return new JsonErrorResponseProvider;
};

$app->add($container['errorHandler.middleware']);



use Chubbyphp\ErrorHandler\ContentTypeResolver;
use Negotiation\Negotiator;
use Psr\Http\Message\ServerRequestInterface as Request;

$resolver = new ContentTypeResolver(new Negotiator);
$resolver->getContentType($request, ['text/html']);



use Chubbyphp\ErrorHandler\AdvancedErrorHandler;

$errorHandler = new AdvancedErrorHandler($resolver, $fallbackProvider, $providers);

$response = $errorHandler($request, $response, $expection);



use Chubbyphp\ErrorHandler\AdvancedErrorHandlerProvider;
use MyProject\ErrorHandler\JsonErrorResponseProvider;
use MyProject\ErrorHandler\XmlErrorResponseProvider;
use Pimple/Container;

$container = new Container();
$container->register(new AdvancedErrorHandlerProvider);

// IMPORTANT: without this definition, the error handler will not work!
$container['errorHandler.defaultProvider'] = function () use ($container) {
    return new JsonErrorResponseProvider;
};

// optional: add more than the default provider
$container->extend('errorHandler.providers', function (array $providers) {
    $providers[] = new XmlErrorResponseProvider;

    return $providers;
});

$app->add($container['errorHandler.middleware']);