xref: /webtrees/app/Http/Middleware/ErrorHandler.php (revision 234e0863a607ff8e5f8e78a7fefe5366f39f8ebf)
1*234e0863SGreg Roach<?php
2*234e0863SGreg Roach
3*234e0863SGreg Roach/**
4*234e0863SGreg Roach * webtrees: online genealogy
5*234e0863SGreg Roach * Copyright (C) 2023 webtrees development team
6*234e0863SGreg Roach * This program is free software: you can redistribute it and/or modify
7*234e0863SGreg Roach * it under the terms of the GNU General Public License as published by
8*234e0863SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*234e0863SGreg Roach * (at your option) any later version.
10*234e0863SGreg Roach * This program is distributed in the hope that it will be useful,
11*234e0863SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*234e0863SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*234e0863SGreg Roach * GNU General Public License for more details.
14*234e0863SGreg Roach * You should have received a copy of the GNU General Public License
15*234e0863SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*234e0863SGreg Roach */
17*234e0863SGreg Roach
18*234e0863SGreg Roachdeclare(strict_types=1);
19*234e0863SGreg Roach
20*234e0863SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21*234e0863SGreg Roach
22*234e0863SGreg Roachuse ErrorException;
23*234e0863SGreg Roachuse Psr\Http\Message\ResponseInterface;
24*234e0863SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
25*234e0863SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
26*234e0863SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
27*234e0863SGreg Roach
28*234e0863SGreg Roachuse function error_reporting;
29*234e0863SGreg Roachuse function restore_error_handler;
30*234e0863SGreg Roachuse function set_error_handler;
31*234e0863SGreg Roach
32*234e0863SGreg Roachclass ErrorHandler implements MiddlewareInterface
33*234e0863SGreg Roach{
34*234e0863SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35*234e0863SGreg Roach    {
36*234e0863SGreg Roach        set_error_handler(callback: $this->errorHandler(...));
37*234e0863SGreg Roach
38*234e0863SGreg Roach        $response = $handler->handle($request);
39*234e0863SGreg Roach
40*234e0863SGreg Roach        restore_error_handler();
41*234e0863SGreg Roach
42*234e0863SGreg Roach        return $response;
43*234e0863SGreg Roach    }
44*234e0863SGreg Roach
45*234e0863SGreg Roach    private function errorHandler(int $errno, string $errstr, string $errfile, int $errline): bool
46*234e0863SGreg Roach    {
47*234e0863SGreg Roach        // Ignore errors that are silenced with '@'
48*234e0863SGreg Roach        if ((error_reporting() & $errno) !== 0) {
49*234e0863SGreg Roach            throw new ErrorException(message: $errstr, code: 0, severity: $errno, filename: $errfile, line: $errline);
50*234e0863SGreg Roach        }
51*234e0863SGreg Roach
52*234e0863SGreg Roach        return true;
53*234e0863SGreg Roach    }
54*234e0863SGreg Roach}
55