1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpJsonRpc\Server\Response\ErrorFormatter; |
4
|
|
|
|
5
|
|
|
use PhpJsonRpc\Server\Response\Error; |
6
|
|
|
use PhpJsonRpc\Server\Response\Contract\ErrorFormatter; |
7
|
|
|
|
8
|
|
|
class DefaultErrorFormatter implements ErrorFormatter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
protected $isDebug; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param bool $isDebug |
17
|
|
|
*/ |
18
|
39 |
|
public function __construct($isDebug = true) |
19
|
|
|
{ |
20
|
39 |
|
$this->isDebug = $isDebug; |
21
|
39 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Error $error |
25
|
|
|
* |
26
|
|
|
* @return \stdClass |
27
|
|
|
*/ |
28
|
|
|
public function formatError(Error $error) |
29
|
|
|
{ |
30
|
|
|
$jsonSerializable = new \stdClass(); |
31
|
|
|
|
32
|
|
|
$jsonSerializable->code = $error->code(); |
33
|
|
|
$jsonSerializable->message = $error->message(); |
34
|
|
|
|
35
|
|
|
if ($error->hasData()) { |
36
|
|
|
$jsonSerializable->data = $error->data(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($this->isDebug) { |
40
|
|
|
$jsonSerializable->debug = $this->buildDebugData($error->exception()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $jsonSerializable; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \Exception $exception |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function buildDebugData(\Exception $exception) |
52
|
|
|
{ |
53
|
|
|
$debugData = [ |
54
|
|
|
'code' => $exception->getCode(), |
55
|
|
|
'message' => $exception->getMessage(), |
56
|
|
|
'type' => get_class($exception), |
57
|
|
|
'file' => $exception->getFile(), |
58
|
|
|
'line' => $exception->getLine(), |
59
|
|
|
'trace' => $exception->getTrace() |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
if ($exception->getPrevious()) { |
63
|
|
|
$debugData = [$debugData]; |
64
|
|
|
$newError = $this->buildDebugData($exception->getPrevious()); |
65
|
|
|
|
66
|
|
|
array_unshift($debugData, $newError); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $debugData; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|