Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
 
7
namespace Whoops\Handler;
8
 
9
use Whoops\Exception\Formatter;
10
 
11
/**
12
 * Catches an exception and converts it to a JSON
13
 * response. Additionally can also return exception
14
 * frames for consumption by an API.
15
 */
16
class JsonResponseHandler extends Handler
17
{
18
    /**
19
     * @var bool
20
     */
21
    private $returnFrames = false;
22
 
23
    /**
24
     * @var bool
25
     */
26
    private $jsonApi = false;
27
 
28
    /**
29
     * Returns errors[[]] instead of error[] to be in compliance with the json:api spec
30
     * @param bool $jsonApi Default is false
31
     * @return static
32
     */
33
    public function setJsonApi($jsonApi = false)
34
    {
35
        $this->jsonApi = (bool) $jsonApi;
36
        return $this;
37
    }
38
 
39
    /**
40
     * @param  bool|null  $returnFrames
41
     * @return bool|static
42
     */
43
    public function addTraceToOutput($returnFrames = null)
44
    {
45
        if (func_num_args() == 0) {
46
            return $this->returnFrames;
47
        }
48
 
49
        $this->returnFrames = (bool) $returnFrames;
50
        return $this;
51
    }
52
 
53
    /**
54
     * @return int
55
     */
56
    public function handle()
57
    {
58
        if ($this->jsonApi === true) {
59
            $response = [
60
                'errors' => [
61
                    Formatter::formatExceptionAsDataArray(
62
                        $this->getInspector(),
688 lars 63
                        $this->addTraceToOutput(),
64
                        $this->getRun()->getFrameFilters()
148 lars 65
                    ),
66
                ]
67
            ];
68
        } else {
69
            $response = [
70
                'error' => Formatter::formatExceptionAsDataArray(
71
                    $this->getInspector(),
688 lars 72
                    $this->addTraceToOutput(),
73
                    $this->getRun()->getFrameFilters()
148 lars 74
                ),
75
            ];
76
        }
77
 
78
        echo json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0);
79
 
80
        return Handler::QUIT;
81
    }
82
 
83
    /**
84
     * @return string
85
     */
86
    public function contentType()
87
    {
88
        return 'application/json';
89
    }
90
}