diff --git a/composer.json b/composer.json index e09f000..4e9489b 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,12 @@ }, "require-dev": { "squizlabs/php_codesniffer": "*", + "satooshi/php-coveralls": "^1.0", "phpunit/phpunit": "5.*", "slim/slim": "^3.0", "ext-pdo_sqlite": "*", - "oscarotero/psr7-middlewares": "^3.16" + "oscarotero/psr7-middlewares": "^3.16", + "codacy/coverage": "^1.0" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Exceptions/ResponseException.php b/src/Exceptions/ResponseException.php index 274ef45..5350652 100644 --- a/src/Exceptions/ResponseException.php +++ b/src/Exceptions/ResponseException.php @@ -36,9 +36,18 @@ class ResponseException extends \Exception */ public function __construct(Errors $errors) { - $this->errors = $errors; + $title = 'Response exception'; + + if (isset( + $errors->getErrors()[0], + $errors->getErrors()[0]->title + )) { + $title = $errors->getErrors()[0]->title; + } - parent::__construct('Response exception'); + parent::__construct($title); + + $this->errors = $errors; } /** diff --git a/src/Response/Errors.php b/src/Response/Errors.php index 4506886..3066797 100644 --- a/src/Response/Errors.php +++ b/src/Response/Errors.php @@ -18,6 +18,7 @@ namespace Phramework\JSONAPI\Client\Response; use Phramework\JSONAPI\Client\Error; +use Psr\Http\Message\ResponseInterface; /** * @author Xenofon Spafaridis @@ -31,6 +32,13 @@ class Errors extends Response */ protected $errors; + public function __construct(ResponseInterface $response) + { + $this->errors = []; + + parent::__construct($response); + } + /** * @return \Phramework\JSONAPI\Client\Error[] */ diff --git a/tests/APP/public/index.php b/tests/APP/public/index.php index f3d2562..93d316f 100755 --- a/tests/APP/public/index.php +++ b/tests/APP/public/index.php @@ -22,11 +22,12 @@ class Ctrl } $c = new \Slim\Container(); + $c['errorHandler'] = function ($c) { return function ($request, $response, \Exception $exception) use ($c) { switch (get_class($exception)) { case \Phramework\Exceptions\NotFoundException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') ->write(json_encode((object) [ 'errors' => [(object) [ @@ -36,7 +37,7 @@ class Ctrl ]] ])); case \Phramework\Exceptions\MissingParametersException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') //->write($exception->getMessage()) ->write(json_encode((object) [ @@ -51,7 +52,7 @@ class Ctrl ]] ])); case \Phramework\Exceptions\IncorrectParameterException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') //->write($exception->getMessage()) ->write(json_encode((object) [ @@ -64,7 +65,7 @@ class Ctrl ]] ])); case \Phramework\Exceptions\IncorrectParametersException::class: - return $c['errors']->withStatus($exception->getCode()) + return $c['response']->withStatus($exception->getCode()) ->withHeader('Content-Type', 'application/json') //->write($exception->getMessage()) ->write(json_encode((object) [ @@ -73,7 +74,7 @@ class Ctrl ])); case \Exception::class: default: - return $c['errors']->withStatus(400) + return $c['response']->withStatus(400) ->withHeader('Content-Type', 'application') //->write($exception->getMessage()) ->write($exception);