1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\ExceptionHandlers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Http\ThrowableResponseInterface; |
22
|
|
|
use Psr\Container\ContainerInterface; |
23
|
|
|
use Throwable; |
24
|
|
|
use Whoops\Handler\PrettyPageHandler; |
25
|
|
|
use Whoops\Run; |
26
|
|
|
use function call_user_func; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Limoncello\Application |
30
|
|
|
* |
31
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
32
|
|
|
*/ |
33
|
|
|
class WhoopsThrowableHtmlHandler extends BaseThrowableHandler |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
* |
38
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
39
|
|
|
*/ |
40
|
|
|
public function createResponse(Throwable $throwable, ContainerInterface $container): ThrowableResponseInterface |
41
|
|
|
{ |
42
|
|
|
$message = 'Internal Server Error'; |
43
|
|
|
|
44
|
|
|
$this->logException($throwable, $container, $message); |
45
|
|
|
|
46
|
|
|
list($isDebug, $appName, $exceptionDumper) = $this->getSettings($container); |
47
|
|
|
|
48
|
|
|
$status = $throwable->getCode() > 0 ? $throwable->getCode() : static::DEFAULT_HTTP_ERROR_CODE; |
49
|
|
|
|
50
|
3 |
|
if ($isDebug === true) { |
51
|
|
|
$run = new Run(); |
52
|
3 |
|
|
53
|
|
|
// If these two options are not used it would work fine with PHP Unit and XDebug, |
54
|
3 |
|
// however it produces output to console under PhpDbg. So we need a couple of |
55
|
|
|
// tweaks to make it work predictably in both environments. |
56
|
3 |
|
// |
57
|
|
|
// this one forbids Whoops spilling output to console |
58
|
3 |
|
$run->writeToOutput(false); |
59
|
2 |
|
// by default after sending error to output Whoops stops execution |
60
|
|
|
// as we want just generated output `string` we instruct not to halt |
61
|
|
|
$run->allowQuit(false); |
62
|
|
|
|
63
|
|
|
$handler = new PrettyPageHandler(); |
64
|
|
|
// without the line below Whoops is too smart and do not produce any output in tests |
65
|
|
|
$handler->handleUnconditionally(true); |
66
|
2 |
|
|
67
|
|
|
if ($exceptionDumper !== null) { |
68
|
|
|
$appSpecificDetails = call_user_func($exceptionDumper, $throwable, $container); |
69
|
2 |
|
$handler->addDataTable("$appName Details", $appSpecificDetails); |
70
|
|
|
} |
71
|
2 |
|
|
72
|
|
|
$handler->setPageTitle("Whoops! There was a problem with '$appName'."); |
73
|
2 |
|
$run->appendHandler($handler); |
74
|
|
|
|
75
|
2 |
|
$html = $run->handleException($throwable); |
76
|
2 |
|
$response = $this->createThrowableHtmlResponse($throwable, $html, $status); |
|
|
|
|
77
|
2 |
|
} else { |
78
|
|
|
$response = $this->createThrowableTextResponse($throwable, $message, $status); |
79
|
|
|
} |
80
|
2 |
|
|
81
|
2 |
|
return $response; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.