Skip to content

Commit

Permalink
Improve ResponseException title #9
Browse files Browse the repository at this point in the history
Fix slim exception response
Add coveralls
  • Loading branch information
nohponex committed Jan 29, 2017
1 parent c9fadbd commit a82f48e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions src/Exceptions/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Response/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Phramework\JSONAPI\Client\Response;

use Phramework\JSONAPI\Client\Error;
use Psr\Http\Message\ResponseInterface;

/**
* @author Xenofon Spafaridis <nohponex@gmail.com>
Expand All @@ -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[]
*/
Expand Down
11 changes: 6 additions & 5 deletions tests/APP/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) [
Expand All @@ -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) [
Expand All @@ -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) [
Expand All @@ -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) [
Expand All @@ -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);
Expand Down

0 comments on commit a82f48e

Please sign in to comment.