$app->get('/product/{id}', function ($id) use ($app) { // retrieve the product $product = getProductFromDb($id); // if the product does not exist, return a 404 error message if (!$product) { $app->abort(404, "Product does not exist."); } // render the product details template return $app['twig']->render('product_details.twig', array('product' => $product)); });
$app->error(function (\Exception $e, $code) use ($app) { // log the error message $app['monolog']->addError($e->getMessage()); // display a custom error message to the user $app->abort(500, "Something went wrong. Please try again later."); });In this example, if an exception occurs in the application, the error function will log the error message and return a 500 error message to the user. The "Application abort" feature in Silex is part of the Symfony HttpFoundation component, which provides a set of classes for HTTP request/response handling.