Skip to content

Instantly share code, notes, and snippets.

@tournasdim
Created June 27, 2014 07:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tournasdim/171b443065936bbb5ef3 to your computer and use it in GitHub Desktop.
Save tournasdim/171b443065936bbb5ef3 to your computer and use it in GitHub Desktop.
A simple example how to handle errors in Silex
use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
$app->error(function (\Exception $e) use ($app) {
if ($e instanceof NotFoundHttpException) {
return $app->json(array('error' => 'Page Not Found'), 404);
}
$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
return $app->json(array('error' => $e->getMessage()), $code);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment