Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
645e2d0
commit f9b1bda
Showing
8 changed files
with
190 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace PhpJsonRpc\Server\Response\Contract; | ||
|
||
use PhpJsonRpc\Server\Response\Error; | ||
|
||
interface ErrorFormatter | ||
{ | ||
/** | ||
* @param Error $error | ||
* | ||
* @return \stdClass | ||
*/ | ||
public function formatError(Error $error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace PhpJsonRpc\Server\Response\ErrorFormatter; | ||
|
||
use PhpJsonRpc\Server\Response\Error; | ||
use PhpJsonRpc\Server\Response\Contract\ErrorFormatter; | ||
|
||
class DefaultErrorFormatter implements ErrorFormatter | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $isDebug; | ||
|
||
/** | ||
* @param bool $isDebug | ||
*/ | ||
public function __construct($isDebug = true) | ||
{ | ||
$this->isDebug = $isDebug; | ||
} | ||
|
||
/** | ||
* @param Error $error | ||
* | ||
* @return \stdClass | ||
*/ | ||
public function formatError(Error $error) | ||
{ | ||
$jsonSerializable = new \stdClass(); | ||
|
||
$jsonSerializable->code = $error->code(); | ||
$jsonSerializable->message = $error->message(); | ||
|
||
if ($error->hasData()) { | ||
$jsonSerializable->data = $error->data(); | ||
} | ||
|
||
if ($this->isDebug) { | ||
$jsonSerializable->debug = $this->buildDebugData($error->exception()); | ||
} | ||
|
||
return $jsonSerializable; | ||
} | ||
|
||
/** | ||
* @param \Exception $exception | ||
* | ||
* @return array | ||
*/ | ||
protected function buildDebugData(\Exception $exception) | ||
{ | ||
$debugData = [ | ||
'code' => $exception->getCode(), | ||
'message' => $exception->getMessage(), | ||
'type' => get_class($exception), | ||
'file' => $exception->getFile(), | ||
'line' => $exception->getLine(), | ||
'trace' => $exception->getTrace() | ||
]; | ||
|
||
if ($exception->getPrevious()) { | ||
$debugData = [$debugData]; | ||
$newError = $this->buildDebugData($exception->getPrevious()); | ||
|
||
array_unshift($debugData, $newError); | ||
} | ||
|
||
return $debugData; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.