PSR-15 compatible middleware for Whoops, the pretty error handler

Overview

PSR-15 middleware for Whoops

A PSR-15 compatible middleware for Whoops, the fantastic pretty error handler for PHP.

Installation

You can install the library using Composer:

composer require franzl/whoops-middleware

Usage

Assuming you are using a PSR-15 compatible middleware dispatcher (such as zend-stratigility, Relay, or broker), all you need to do is add the middleware class to your pipeline / broker / dispatcher ...

This might look as follows:

Stratigility

$pipe->pipe(new \Franzl\Middleware\Whoops\WhoopsMiddleware)

Relay

$queue = [];
// ...
$queue[] = new \Franzl\Middleware\Whoops\WhoopsMiddleware;
$relay = new Relay($queue);

broker

$broker->always(\Franzl\Middleware\Whoops\WhoopsMiddleware::class)
Comments
  • Responding to requested content type

    Responding to requested content type

    At the moment the library always returns an HtmlResponse for the Whoops handler. I made a workaround which sets a default content type for the response but this is a bit of a hack as it should probably insert best fit according to the request accept header. Please consider handling, but if you want a PR in the meantime let me know.

    https://github.com/MrHash/whoops-middleware/commit/e4f65776719a1ca66d8a3df1cadc3406cd1a3602

    opened by MrHash 5
  • PSR15 middleware support

    PSR15 middleware support

    I am interested in support for PSR15 middleware.

    Despite the fact that PSR 15 is not yet finished I already started on an approach to support version ^0.4.

    Have a look at timtegeler/whoops-middleware@47c18c1ca85b2bc9b5ad4d14c2f2d8146fb9e800

    Perhaps you can review my approach and share your thoughts with me. :)

    opened by fetchandadd 4
  • Refactoring: Introduced dependency injection for component decoupling…

    Refactoring: Introduced dependency injection for component decoupling…

    …, using WhoopsRunFactory for creating WhoopsRun instance, added unit tests

    I know this is a major change to your library and comes out of the blue, but I think this could gain some benefits. I'm curious about what you're saying. :)

    In the last days I realized, that if I want to integrate franzliedke/whoops-middleware in one of my projects I would have some problems. My desire is to use franzliedke/whoops-middleware on development and production environment.

    Because of that, I would need the ability to manipulate the creation of Whoops\Run like in this code example from one of Whoops issues:

    error_reporting(E_ALL);
    $run = new \Whoops\Run;
    if ($debug) {
        $run->pushHandler(new \Whoops\Handler\PrettyPageHandler);
        assert_options(ASSERT_ACTIVE, true);
    } else {
        $run->pushHandler(function($e){
            $myApp->outputFriendlyError();
            $myApp->sendDevEmail();
        });
    }
    $run->register();
    

    But this logic is inside of WhoopsRunner and cannot be manipulate because of the static call of getWhoopsInstance from the Middlewares.

    So I started to rewrite a little bit to proof my ideas. More and more ideas came into my mind and I came up with a major refactoring. Here is the list of changes I made:

    • Using dependency injection to inject components e.g. WhoopsRunner instead of calling static methods. This has the advantage to decouple the logic and simpler unit testing.
    /**
    * @param WhoopsRunner $whoopsRunner
    */
    public function __construct(WhoopsRunner $whoopsRunner)
    {
    	$this->whoopsRun = $whoopsRunner;
    }
    
    • To circumvent the dependency hell - if a library user wants to use the Middleware without worrying about the creation of Whoops\Run and doesn't use a DI-Framework - I added factory methods.
    /**
    * @return Middleware
    */
    public static function createNewInstance()
    {
    	return new Middleware(WhoopsRunner::createNewInstance());
    }
    
    • I splitted WhoopsRunner into to two sperate components WhoopsRunner and WhoopsRunFactory. WhoopsRunFactory implements WhoopsRunFactoryInterface which is now a dependency of WhoopsRunner. WhoopsRunFactory is responsible for creating instances of Whoops\Run. Because of that a library user has the possibility to implement WhoopsRunFactoryInterface on his own, to alter the creation of Whoops\Run.
    interface WhoopsRunFactoryInterface
    {
        /**
         * @param ServerRequestInterface $request
         * @return Run
         */
        public function getWhoopsInstance(ServerRequestInterface $request);
    
    }
    
    • I wrote unit tests for every component and achieved a 100% code coverage. This needed a little bit of refactoring which is highly related to my the first item of this list. E.g. I implemented a CliDetector which is a dependency of WhoopsRunFactory. It detects if the server api is equal to 'cli'. This gave me the ability to mock CliDetector and test lines of code in WhoopsRunFactory which otherwise I could't reach.
    class CliDetector
    {
        /**
         * @return bool
         */
        public function isCli()
        {
            return php_sapi_name() === 'cli';
        }
    }
    
    • To 'hide' components from the library user that are not part of the library interface I moved them into a insides directory.

    I didn't add more than autogenerated PHPDoc and also didn't adjust the README. Before I would do that, I await your response.

    opened by fetchandadd 3
  • Fix return StringResponse in WhoopsRunner handle

    Fix return StringResponse in WhoopsRunner handle

    Handle method return HtmlResponse instead of StringResponse which does not exist anymore in Zend/Diactoros : https://github.com/zendframework/zend-diactoros/pull/61

    opened by Fooriva 3
  • Adding (needed) Stratigility interface

    Adding (needed) Stratigility interface

    The ErrorMiddleware (that is designed to work with Stratigility) needs to have 4 required parameters if we want it to be detected as an error middleware by Stratigility. Otherwise, it is detected as a normal middleware and breaks.

    It is because of these lines of code:

    https://github.com/zendframework/zend-stratigility/blob/61adbe76af9a3a1f5d2e5df7b5182daa0cf472d4/src/Dispatch.php#L61-L70

    I simply removed the "= null" from the 4th parameter to make it required.

    opened by moufmouf 3
Releases(2.0.0)
  • 2.0.0(Jan 3, 2021)

    Time for some PHP and dependency maintenance.

    Summary

    • Add support for PHP 8, drop support for PHP <7.2.
    • Rely on middleware/utils package to pick an available PSR-7 implementation (#12, #13), don't force Laminas on people.
    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Jan 7, 2020)

    Full changelog:

    • Fixed:
      • Respond with appropriate Content-Type header for all supported response formats (#11)
      • Always use first match when multiple accepted content types are supported
    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Mar 30, 2019)

  • 1.0.0(Apr 11, 2018)

    As the interfaces changed slightly before the final version of PSR-15 was published, this release brings support for the latest version (see #9).

    All other middleware adapters were removed. Please user PSR-15, it's the standard!

    Please check out the README for the current interface.

    Source code(tar.gz)
    Source code(zip)
  • 0.5.0(Feb 1, 2017)

    Thanks to @timtegeler, we now support PSR-15 compatible (as of yet, it's still in review) middleware.

    Oh, and I also added an official license, this is now licensed under MIT. If this is possible, I hereby want to clarify that I consider all previous versions licensed under MIT as well, so you are not in trouble for using them.

    Source code(tar.gz)
    Source code(zip)
  • 0.4.1(Apr 27, 2016)

  • 0.4.0(Apr 18, 2016)

    The first release only had an error middleware for use with Zend Stratigility. This new release also supports other middleware stacks, such as Relay.

    Full changelog:

    • Format negotiation: Output is now formatted according to the preferred content type (#4).
    • Fixed:
      • Whoops is not allowed to exit the process anymore, this should be taken care of by the middleware pipeline.
    Source code(tar.gz)
    Source code(zip)
  • 0.3.1(Feb 26, 2016)

    Full changelog:

    • Fixed:
      • The ErrorMiddleware didn't completely fulfill Stratigility's requirements: all four parameters needed to be required. (#3).
    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Jan 29, 2016)

  • 0.2.0(Oct 31, 2015)

    The first release only had an error middleware for use with Zend Stratigility. This new release also supports other middleware stacks, such as Relay.

    Full changelog:

    • Classical Middleware support: The Middleware class now implements the classical three-arguments signature as known from Relay and others.
    • Changed:
      • The class for use with Stratigility has been renamed from Middleware to ErrorMiddleware.
    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Oct 14, 2015)

Owner
Franz Liedke
Franz Liedke
A simple and flexible PHP middleware dispatcher based on PSR-7, PSR-11, and PSR-15

Woohoo Labs. Harmony Woohoo Labs. Harmony is a PSR-15 compatible middleware dispatcher. Harmony was born to be a totally flexible and almost invisible

Woohoo Labs. 153 Sep 5, 2022
PSR-7 middleware foundation for building and dispatching middleware pipelines

laminas-stratigility From "Strata", Latin for "layer", and "agility". This package supersedes and replaces phly/conduit. Stratigility is a port of Sen

Laminas Project 47 Dec 22, 2022
Disable Google's FLoC with help of PSR-15 middleware

Disable Google's FLoC with PSR-15 middleware This package will help you disable Google's FLoC. Installation You can install the package via composer:

P7V 9 Dec 14, 2022
PSR-15 middleware to geolocate the client using the ip address

middlewares/geolocation ![SensioLabs Insight][ico-sensiolabs] Middleware to geolocate the client using the ip address and Geocoder and save the result

Middlewares 10 Mar 22, 2022
A PSR-15 middleware adapter for react/http

A PSR-15 middleware adapter for react/http Wraps PSR-15 middleware into coroutines using RecoilPHP making them usable within react/http as middleware.

Friends of ReactPHP 22 Nov 12, 2022
A PSR-15 middleware to handle content negotiation

Content negotiation middleware Motivation Packages like middlewares/negotiation do a very good job to detect the correct content type based on the Acc

Luís Cobucci 47 Nov 16, 2022
An internal redirect mechanism for PSR-15 middleware stacks

HTTP Request Forwarder The aim of this library is to make it possible to pass the HTTP request to another handler, creating a so-called internal redir

Zoltan Kovago 0 Jul 27, 2022
Provides a Middleware to integration Tideways into Symfony Messenger Processing

Tideways Middleware for Symfony Messenger This package is currently under development and might be moved into the Tideways PHP Extension or stay indep

Tideways 6 Jul 5, 2022
A lightweight middleware to make api routing session capable.

Laravel stateless session A lightweight middleware to make api routing session capable. Installing $ composer require overtrue/laravel-stateless-sessi

安正超 17 Jul 6, 2022
Use middleware to decorate method calls within your application code.

Laravel Middlewarize ?? Chain of Responsibility Design Pattern In Laravel Apps ?? You can use middlewares to decorate any method calls on any object.

Iman 99 Jan 1, 2023
Stepup Middleware - This component is part of "Step-up Authentication as-a Service".

Step-up Middleware This component is part of "Step-up Authentication as-a Service". See Stepup-Deploy for an overview and installation instructions fo

OpenConext 4 Nov 2, 2022
Middleware to provide IP filtering

middlewares/firewall Middleware to provide IP filtering using M6Web/Firewall. Requirements PHP >= 7.2 A PSR-7 http library A PSR-15 middleware dispatc

Middlewares 10 Dec 1, 2022
A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of the individual components, but also of the framework.

chubbyphp-framework Description A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of th

chubbyphp 106 Dec 9, 2022
The efficient and elegant, PSR-7 compliant JSON:API 1.1 client library for PHP

Woohoo Labs. Yang Woohoo Labs. Yang is a PHP framework which helps you to communicate with JSON:API servers more easily. Table of Contents Introductio

Woohoo Labs. 160 Oct 16, 2022
OpenAPI(v3) Validators for Symfony http-foundation, using `league/openapi-psr7-validator` and `symfony/psr-http-message-bridge`.

openapi-http-foundation-validator OpenAPI(v3) Validators for Symfony http-foundation, using league/openapi-psr7-validator and symfony/psr-http-message

n1215 2 Nov 19, 2021
It validates PSR-7 messages (HTTP request/response) against OpenAPI specifications

OpenAPI PSR-7 Message (HTTP Request/Response) Validator This package can validate PSR-7 messages against OpenAPI (3.0.x) specifications expressed in Y

The League of Extraordinary Packages 421 Jan 3, 2023
Tukio is a complete and robust implementation of the PSR-14 Event Dispatcher specification

Tukio is a complete and robust implementation of the PSR-14 Event Dispatcher specification. It supports normal and debug Event Dispatchers, both runtime and compiled Providers, complex ordering of Listeners, and attribute-based registration on PHP 8.

Larry Garfield 70 Dec 19, 2022
A super simple, clean and pretty error handler that replace the default error handler of PHP. You need only include this file!

php-custom-error-handler A super simple, clean and pretty error handler that replace the default error handler of PHP. You need just include only this

null 6 Nov 7, 2022
PSR-15 middleware to use Whoops as error handler

middlewares/whoops Middleware to use Whoops as error handler. Requirements PHP >= 7.2 A PSR-7 http library A PSR-15 middleware dispatcher Installation

Middlewares 31 Jun 23, 2022
PHP whoops error on slim framework

Slim whoops PHP whoops error on slim framework Status Installation Install the composer curl -sS https://getcomposer.org/installer | php Edit compose

Zeuxis 130 Nov 25, 2022