Skip to content

Commit

Permalink
Introduce debug flag for VndErrorResponse
Browse files Browse the repository at this point in the history
If true, all exceptions messages are exposed in the repsonse.
  • Loading branch information
jsor committed Oct 14, 2014
1 parent e1ee111 commit 1e5eba8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ class ExceptionConverter implements HttpKernelInterface
{
private $app;
private $prettyPrint;
private $debug;
private $passThroughCatch;

public function __construct(HttpKernelInterface $app,
$prettyPrint = false,
$debug = false,
$passThroughCatch = false)
{
$this->app = $app;
$this->prettyPrint = (bool) $prettyPrint;
$this->debug = (bool) $debug;
$this->passThroughCatch = (bool) $passThroughCatch;
}

Expand All @@ -34,7 +37,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
throw $exception;
}

return VndErrorResponse::fromException($exception, $this->prettyPrint);
return VndErrorResponse::fromException($exception, $this->prettyPrint, $this->debug);
}
}
}
14 changes: 7 additions & 7 deletions src/Response/VndErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@

class VndErrorResponse extends HalResponse
{
public function __construct(Hal $hal, $status = 500, $headers = array(), $prettyPrint = false)
public function __construct(Hal $hal, $status = 500, $headers = array(), $prettyPrint = false, $debug = false)
{
parent::__construct($hal, $status, $headers, $prettyPrint);

$this->headers->set('Content-Type', 'application/vnd.error+json');
}

public static function create($hal = null, $status = 500, $headers = array(), $prettyPrint = false)
public static function create($hal = null, $status = 500, $headers = array(), $prettyPrint = false, $debug = false)
{
return new static($hal, $status, $headers, $prettyPrint);
return new static($hal, $status, $headers, $prettyPrint, $debug);
}

public static function fromException(\Exception $exception, $prettyPrint = false)
public static function fromException(\Exception $exception, $prettyPrint = false, $debug = false)
{
$statusCode = 500;
$headers = [];
Expand All @@ -34,11 +34,11 @@ public static function fromException(\Exception $exception, $prettyPrint = false

if ($exception instanceof HalException) {
$hal = $exception->getHal();
} elseif ($exception instanceof HttpExceptionInterface) {
} elseif ($debug || $exception instanceof HttpExceptionInterface) {
$hal = new Hal(null, ['message' => $exception->getMessage()]);
} else {
// Discard exception messages from exceptions
// not implementing HttpExceptionInterface
// Discard exception messages from exceptions not implementing
// HttpExceptionInterface (if $debug is false)
$hal = new Hal();
}

Expand Down
28 changes: 28 additions & 0 deletions tests/ExceptionConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,34 @@ public function it_discards_standard_exception_message()
);
}

/** @test */
public function it_exposes_standard_exception_message_when_debug_is_true()
{
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');

$kernel
->expects($this->once())
->method('handle')
->will($this->throwException(new \Exception('Custom error message')));

$app = new ExceptionConverter($kernel, false, true);

$request = new Request();
$request->attributes->set('_format', 'json');

$response = $app->handle($request)->prepare($request);

$this->assertSame(500, $response->getStatusCode());
$this->assertJsonStringEqualsJsonString(
json_encode(
[
'message' => 'Custom error message',
]
),
$response->getContent()
);
}

/** @test */
public function it_rethrows_exception_if_catch_is_false()
{
Expand Down

0 comments on commit 1e5eba8

Please sign in to comment.