Commit 0c67f90c authored by Fabien Potencier's avatar Fabien Potencier

simplified the callback the developer needs to create when handling errors...

simplified the callback the developer needs to create when handling errors through Application::error()
parent b6dca08c
...@@ -366,25 +366,24 @@ which takes an ``Exception`` argument and returns a response:: ...@@ -366,25 +366,24 @@ which takes an ``Exception`` argument and returns a response::
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
$app->error(function (\Exception $e) { $app->error(function (\Exception $e, $code) {
return new Response('We are sorry, but something went terribly wrong.', 500); return new Response('We are sorry, but something went terribly wrong.', $code);
}); });
You can also check for specific errors by using ``instanceof``, and handle You can also check for specific errors by using the ``$code`` argument, and
them differently:: handle them differently::
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
$app->error(function (\Exception $e) { $app->error(function (\Exception $e, $code) {
if ($e instanceof NotFoundHttpException) { switch ($code) {
return new Response('The requested page could not be found.', 404); case 404:
$message = 'The requested page could not be found.';
default:
$message = 'We are sorry, but something went terribly wrong.';
} }
$code = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500; return new Response($message, $code);
return new Response('We are sorry, but something went terribly wrong.', $code);
}); });
If you want to set up logging you can use a separate error handler for that. If you want to set up logging you can use a separate error handler for that.
......
...@@ -20,6 +20,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; ...@@ -20,6 +20,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
...@@ -230,7 +231,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -230,7 +231,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{ {
$this['dispatcher']->addListener(SilexEvents::ERROR, function (GetResponseForErrorEvent $event) use ($callback) { $this['dispatcher']->addListener(SilexEvents::ERROR, function (GetResponseForErrorEvent $event) use ($callback) {
$exception = $event->getException(); $exception = $event->getException();
$result = $callback($exception); $code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
$result = $callback($exception, $code);
if (null !== $result) { if (null !== $result) {
$event->setStringResponse($result); $event->setStringResponse($result);
......
...@@ -15,6 +15,7 @@ use Silex\Application; ...@@ -15,6 +15,7 @@ use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Error handler test cases. * Error handler test cases.
...@@ -97,16 +98,24 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -97,16 +98,24 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{ {
$app = new Application(); $app = new Application();
$app->match('/foo', function () { $app->match('/500', function () {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
$app->error(function ($e) { $app->match('/404', function () {
return new Response('foo exception handler'); throw new NotFoundHttpException('foo exception');
}); });
$request = Request::create('/foo'); $app->error(function ($e, $code) {
$this->checkRouteResponse($app, '/foo', 'foo exception handler'); return new Response('foo exception handler', $code);
});
$response = $this->checkRouteResponse($app, '/500', 'foo exception handler');
$this->assertEquals(500, $response->getStatusCode());
$request = Request::create('/404');
$response = $app->handle($request);
$this->assertEquals(404, $response->getStatusCode());
} }
public function testMultipleErrorHandlers() public function testMultipleErrorHandlers()
...@@ -211,5 +220,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -211,5 +220,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
$request = Request::create($path, $method); $request = Request::create($path, $method);
$response = $app->handle($request); $response = $app->handle($request);
$this->assertEquals($expectedContent, $response->getContent(), $message); $this->assertEquals($expectedContent, $response->getContent(), $message);
return $response;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment