use Symfony\Component\Debug\ErrorHandler; use Symfony\Component\Debug\ExceptionHandler; $app = new Silex\Application(); // Register the error handler and exception handler ErrorHandler::register(); ExceptionHandler::register(); // Define a custom error message for a 404 error $app->error(function (\Exception $e, Request $request, $code) use ($app) { if ($code == 404) { return $app->json(array('message' => 'Page not found'), 404); } }); $app->run();
use Symfony\Component\Debug\ErrorHandler; use Symfony\Component\Debug\ExceptionHandler; $app = new Silex\Application(); // Register the error handler and exception handler ErrorHandler::register(); ExceptionHandler::register(); // Define a custom error message for any error $app->error(function (\Exception $e, Request $request, $code) use ($app) { $message = 'An error occurred: ' . $e->getMessage(); return $app->json(array('message' => $message), $code); }); $app->run();In both examples, the Symfony\Component\Debug package library is used to register the error handler and exception handler.