Giter VIP home page Giter VIP logo

embryo-error's Introduction

Embryo Error

PSR-15 middleware to handle errors. This handler inspired by Slim. It receives all uncaught PHP exceptions and converts them to a new HTTP response with the status code.

Requirements

Install

Using Composer:

$ composer require davidecesarano/embryo-error

Usage

You may quickly test this using the built-in PHP server going to http://localhost:8000.

use Embryo\Error\ErrorHandler;
use Embryo\Error\Middleware\ErrorHandlerMiddleware;
use Embryo\Http\Factory\{ResponseFactory, ServerRequestFactory};
use Embryo\Http\Server\RequestHandler;
use Embryo\Log\StreamLogger;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};

// Set path log files and stream logger
$logPath = '/path/to/logs';
$logger = new StreamLogger($logPath);

// Set error handler
$errorHandler = (new ErrorHandler)->setLogger($logger);

// Set PSR Request and Response
$request = (new ServerRequestFactory)->createServerRequestFromServer();
$response = (new ResponseFactory)->createResponse(200);

// Set custom middleware with exception
class TestMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        throw new \Exception('This is a test error!');
    }
}

// ErrorHandler should always be the first middleware in the stack!
$middleware = new RequestHandler([
    (new ErrorHandlerMiddleware)->setErrorHandler($errorHandler),
    new TestMiddleware
]);


$response = $middleware->dispatch($request, $response);

Options

__construct(bool $displayDetails = true, bool $logErrors = true)

The error handler includes detailed information on error diagnostics and saving to a log file. To enable this you need to set the displayDetails and logErrors setting to true.

setLogger(LoggerInterface $logger)

You must set a PSR logger if logErrors is set to true.

setRenderer(ErrorRendererInterface $renderer = null, Throwable $exception)

You can set up a custom renderer for printing errors.

use Embryo\Error\Interfaces\ErrorRendererInterface;
use Psr\Http\Message\{ServerRequestInterface, ResponseInterface};

class CustomRenderer implements ErrorRendererInterface 
{
    public function render(ServerRequestInterface $request, ResponseInterface $response, \Throwable $exception): ResponseInterface
    {
        // custom logic...
        return $response;
    }
}

$errorHandler = (new ErrorHandler)->setRenderer(CustomRenderer::class);

embryo-error's People

Contributors

davidecesarano avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.