HosipLan

Untitled

Jul 9th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. class ErrorPresenter extends Presenter
  2. {
  3.  
  4.     /**
  5.      * @var \Monolog\Logger
  6.      * @inject
  7.      */
  8.     public $logger;
  9.  
  10.     /**
  11.      * @param \Exception $exception
  12.      * @return void
  13.      */
  14.     public function renderDefault($exception)
  15.     {
  16.         if ($exception instanceof Nette\Application\BadRequestException) {
  17.             if ($exception instanceof Nette\Application\ForbiddenRequestException && $this->isFromAdmin()) {
  18.                 $this->setView('403');
  19.             } else {
  20.                 $this->setView('4xx');
  21.             }
  22.  
  23.             $this->template->statusCode = $exception->getCode();
  24.             $this->accessLog($exception);
  25.  
  26.         } elseif ($exception instanceof Kdyby\Redis\SessionHandlerException) {
  27.             if (!$this->getHttpResponse()->isSent()) {
  28.                 $this->getHttpResponse()->setCode(429); // 429 Too Many Requests
  29.             }
  30.  
  31.             $this->setView('429');
  32.             $this->accessLog($exception, 429);
  33.  
  34.         } else {
  35.             $this->setView('500'); // load template 500.latte
  36.             Debugger::log($exception, Debugger::ERROR); // and log exception
  37.         }
  38.  
  39.         if ($this->isAjax()) { // AJAX request? Just note this error in payload.
  40.             $this->payload->error = TRUE;
  41.             $this->terminate();
  42.         }
  43.     }
  44.  
  45.  
  46.  
  47.     protected function accessLog(\Exception $exception, $code = NULL)
  48.     {
  49.         $projectRoot = dirname(dirname(dirname(__DIR__))) . '/';
  50.  
  51.         // log to access.log
  52.         $this->logger->addInfo(
  53.             sprintf('HTTP code %d: %s in %s:%d at %s',
  54.                 $code ?: $exception->getCode(),
  55.                 $exception->getMessage(),
  56.                 str_replace($projectRoot, '', $exception->getFile()),
  57.                 $exception->getLine(),
  58.                 (string) $this->getHttpRequest()->getUrl()
  59.             ),
  60.             [
  61.                 'referer' => (string) $this->getHttpRequest()->getReferer(),
  62.                 'channel' => 'access',
  63.             ]
  64.         );
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment