Passed
Pull Request — master (#11)
by Joao
01:49
created

JsonLimitedResponseHandler::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 0
dl 0
loc 22
ccs 0
cts 10
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace ByJG\RestServer\Whoops;
8
9
use Whoops\Exception\Formatter;
10
use Whoops\Handler\Handler;
11
use Whoops\Handler\JsonResponseHandler as ParentJsonErrorHandler;
12
13
/**
14
 * Catches an exception and converts it to a JSON
15
 * response. Additionally can also return exception
16
 * frames for consumption by an API.
17
 */
18
class JsonLimitedResponseHandler extends ParentJsonErrorHandler
19
{
20
21
    use WhoopsDebugTrait;
22
    use WhoopsHeaderTrait;
23
24
    /**
25
     * @return int
26
     */
27
    public function handle()
28
    {
29
        $errorData = Formatter::formatExceptionAsDataArray(
30
            $this->getInspector(),
31
            false
32
        );
33
34
        $refClass = new \ReflectionClass($errorData["type"]);
35
        $className = $refClass->getShortName();
36
37
        $response = array(
38
            'error' => [
39
                "type" => $className,
40
                "message" => $errorData["message"]
41
            ]
42
        );
43
44
        $this->setProperHeader($this->getException());
45
46
        echo json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0);
47
48
        return Handler::QUIT;
49
    }
50
}
51