1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace demo\Errors;
- use Silex\Application;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- class GenericErrorHandler {
- /**
- * @var \Silex\Application
- */
- protected $app;
- public function __construct(Application $app) {
- $this->app = $app;
- }
- /* Custom error handlers registered with error() take precedence over the
- built-in error handler provider by Silex, but the formatted error messages it
- provides can be accessed in debug mode by returning based on $app['debug']
- like this.
- */
- public function __invoke(\Exception $e, Request $request, $code) {
- // Use the default handler in debug mode.
- if ($this->app['debug']) {
- return;
- }
- // Use our error formats otherwise.
- // 404.html, or 40x.html, or 4xx.html, or error.html
- $templates = [
- 'errors/' . $code . '.html.twig',
- 'errors/' . substr($code, 0, 2) . 'x.html.twig',
- 'errors/' . substr($code, 0, 1) . 'xx.html.twig',
- 'errors/default.html.twig',
- ];
- /** @var \Twig_Environment $twig */
- $twig = $this->app['twig'];
- /** @var \Twig_Template $template */
- $template = $twig->resolveTemplate($templates);
- return new Response($template->render(['code' => $code . ' : ' . $e->getMessage()]));
- }
- }
|