xref: /webtrees/app/Http/Middleware/HandleExceptions.php (revision 5fb051e95bc3d8a26af94d588bd85bf2d37f583c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\Middleware;
21
22use Fig\Http\Message\RequestMethodInterface;
23use Fig\Http\Message\StatusCodeInterface;
24use Fisharebest\Localization\Locale\LocaleEnUs;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Log;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\MiddlewareInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31use Symfony\Component\HttpKernel\Exception\HttpException;
32use Throwable;
33
34use function app;
35use function dirname;
36use function ob_end_clean;
37use function ob_get_level;
38use function response;
39use function str_replace;
40use function view;
41
42use const PHP_EOL;
43
44/**
45 * Middleware to handle and render errors.
46 */
47class HandleExceptions implements MiddlewareInterface, StatusCodeInterface
48{
49    use ViewResponseTrait;
50
51    /**
52     * @param ServerRequestInterface  $request
53     * @param RequestHandlerInterface $handler
54     *
55     * @return ResponseInterface
56     * @throws Throwable
57     */
58    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
59    {
60        try {
61            return $handler->handle($request);
62        } catch (HttpException $exception) {
63            // The router added the tree attribute to the request, and we need it for the error response.
64            $request = app(ServerRequestInterface::class) ?? $request;
65
66            return $this->httpExceptionResponse($request, $exception);
67        } catch (Throwable $exception) {
68            // Exception thrown while buffering output?
69            while (ob_get_level() > 0) {
70                ob_end_clean();
71            }
72
73            // The Router middleware may have added a tree attribute to the request.
74            // This might be usable in the error page.
75            if (app()->has(ServerRequestInterface::class)) {
76                $request = app(ServerRequestInterface::class) ?? $request;
77            }
78
79            // No locale set in the request?
80            if ($request->getAttribute('locale') === null) {
81                $request = $request->withAttribute('locale', new LocaleEnUs());
82                app()->instance(ServerRequestInterface::class, $request);
83            }
84
85            // Show the exception in a standard webtrees page (if we can).
86            try {
87                return $this->unhandledExceptionResponse($request, $exception);
88            } catch (Throwable $e) {
89            }
90
91            // Show the exception in a tree-less webtrees page (if we can).
92            try {
93                $request = $request->withAttribute('tree', null);
94
95                return $this->unhandledExceptionResponse($request, $exception);
96            } catch (Throwable $e) {
97            }
98
99            // Show the exception in an error page (if we can).
100            try {
101                $this->layout = 'layouts/error';
102
103                return $this->unhandledExceptionResponse($request, $exception);
104            } catch (Throwable $e) {
105            }
106
107            // Show a stack dump.
108            return response((string) $exception, StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
109        }
110    }
111
112    /**
113     * @param ServerRequestInterface $request
114     * @param HttpException          $exception
115     *
116     * @return ResponseInterface
117     */
118    private function httpExceptionResponse(ServerRequestInterface $request, HttpException $exception): ResponseInterface
119    {
120        $tree = $request->getAttribute('tree');
121
122        if ($request->getHeaderLine('X-Requested-With') !== '') {
123            $this->layout = 'layouts/ajax';
124        }
125
126        return $this->viewResponse('components/alert-danger', [
127            'alert' => $exception->getMessage(),
128            'title' => $exception->getMessage(),
129            'tree'  => $tree,
130        ], $exception->getStatusCode());
131    }
132
133    /**
134     * @param ServerRequestInterface $request
135     * @param Throwable              $exception
136     *
137     * @return ResponseInterface
138     */
139    private function unhandledExceptionResponse(ServerRequestInterface $request, Throwable $exception): ResponseInterface
140    {
141        $this->layout = 'layouts/default';
142
143        // Create a stack dump for the exception
144        $base_path = dirname(__DIR__, 3);
145        $trace     = $exception->getMessage() . ' ' . $exception->getFile() . ':' . $exception->getLine() . PHP_EOL . $exception->getTraceAsString();
146        $trace     = str_replace($base_path, '…', $trace);
147
148        try {
149            Log::addErrorLog($trace);
150        } catch (Throwable $exception) {
151            // Must have been a problem with the database.  Nothing we can do here.
152        }
153
154        if ($request->getHeaderLine('X-Requested-With') !== '') {
155            // If this was a GET request, then we were probably fetching HTML to display, for
156            // example a chart or tab.
157            if ($request->getMethod() === RequestMethodInterface::METHOD_GET) {
158                $status_code = StatusCodeInterface::STATUS_OK;
159            } else {
160                $status_code = StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR;
161            }
162
163            return response(view('components/alert-danger', ['alert' => $trace]), $status_code);
164        }
165
166        return $this->viewResponse('errors/unhandled-exception', [
167            'title' => 'Error',
168            'error' => $trace,
169            'request' => $request,
170            'tree'  => null,
171        ], StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
172    }
173}
174