Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ExceptionHandler
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Middleware;
6
7use Engelsystem\Exceptions\Handler as ExceptionsHandler;
8use Psr\Container\ContainerInterface;
9use Psr\Http\Message\ResponseInterface;
10use Psr\Http\Message\ServerRequestInterface;
11use Psr\Http\Server\MiddlewareInterface;
12use Psr\Http\Server\RequestHandlerInterface;
13use Throwable;
14
15class ExceptionHandler implements MiddlewareInterface
16{
17    public function __construct(protected ContainerInterface $container)
18    {
19    }
20
21    /**
22     * Handles any exceptions that occurred inside other middleware while returning it to the default response handler
23     *
24     * Should be added at the beginning
25     */
26    public function process(
27        ServerRequestInterface $request,
28        RequestHandlerInterface $handler
29    ): ResponseInterface {
30        try {
31            return $handler->handle($request);
32        } catch (Throwable $e) {
33            /** @var ExceptionsHandler $handler */
34            $handler = $this->container->get('error.handler');
35            $content = $handler->exceptionHandler($e, true);
36
37            return response($content, 500);
38        }
39    }
40}