简体   繁体   中英

Slim 4 catching php warnings, notices & errors

I just started using the slim framework and want to catch php warnings, notices and errors with a custom error handler.

ErrorHandlerMiddleware.php

<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class ErrorHandlerMiddleware implements MiddlewareInterface {

    var $errtext;

    public function __construct() {}

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        
        $errorTypes = E_ALL;
        set_error_handler(function ($errno, $errstr, $errfile, $errline) {

            $this->errtext = $errno;
            switch ($errno) {
                
                case E_USER_ERROR:
                    
                    //do something
                    break;

                case E_USER_WARNING:
                    
                    //do something
                    break;

                default:

                    //do something
                    break;

            }
            return true;

        }, $errorTypes );
        
        $request = $request->withAttribute('error',  $this->errtext);
        return $handler->handle($request);

    }
}

index.php

<?php 

    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    use Slim\Factory\AppFactory;
    use App\Middleware\ErrorHandlerMiddleware;
    
    require_once("vendor/autoload.php");

    $app = AppFactory::create();
    $app->setBasePath("/data");
    $app->addRoutingMiddleware();
    
    $app->get('/', function (Request $request, Response $response) {
        $response->getBody()->write('<a href="hello/world">Try /hello/world</a>');
        return $response;
    });

    $app->add(ErrorHandlerMiddleware::class);
    $errorMiddleware = $app->addErrorMiddleware(true, true, true);
    $app->run();

The custom error handler is working (php warnings are not shown anymore). But I want to add some custom variable to the output modified by the error handler. This should be done by

$request = $request->withAttribute('error',  $this->errtext);
$request->getAttribute('error'); 

but the error attribute is null.

Edit: When you define a callback function, then $this in not in the same scope. Also the middleware instance itself will not be "resumed" when an error occurs. Another approach would be to throw a custom Exception to catch and render it within another middleware.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM