]> BookStack Code Mirror - bookstack/blob - app/Exceptions/WhoopsBookStackPrettyHandler.php
Added testing to cover debug view
[bookstack] / app / Exceptions / WhoopsBookStackPrettyHandler.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use Whoops\Handler\Handler;
6
7 class WhoopsBookStackPrettyHandler extends Handler
8 {
9
10     /**
11      * @return int|null A handler may return nothing, or a Handler::HANDLE_* constant
12      */
13     public function handle()
14     {
15         $exception = $this->getException();
16
17         echo view('errors.debug', [
18             'error' => $exception->getMessage(),
19             'errorClass' => get_class($exception),
20             'trace' => $exception->getTraceAsString(),
21             'environment' => $this->getEnvironment(),
22         ])->render();
23
24         return Handler::QUIT;
25     }
26
27     protected function safeReturn(callable $callback, $default = null) {
28         try {
29             return $callback();
30         } catch (\Exception $e) {
31             return $default;
32         }
33     }
34
35     protected function getEnvironment(): array
36     {
37         return [
38             'PHP Version' => phpversion(),
39             'BookStack Version' => $this->safeReturn(function() {
40                 $versionFile = base_path('version');
41                 return trim(file_get_contents($versionFile));
42             }, 'unknown'),
43             'Theme Configured' => $this->safeReturn(function() {
44                     return config('view.theme');
45                 }) ?? 'None',
46         ];
47     }
48 }