tournasdim
6/27/2014 - 7:36 AM

A simple example how to handle errors in Silex

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);
});