Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ErrorPresenter extends Presenter
- {
- /**
- * @var \Monolog\Logger
- * @inject
- */
- public $logger;
- /**
- * @param \Exception $exception
- * @return void
- */
- public function renderDefault($exception)
- {
- if ($exception instanceof Nette\Application\BadRequestException) {
- if ($exception instanceof Nette\Application\ForbiddenRequestException && $this->isFromAdmin()) {
- $this->setView('403');
- } else {
- $this->setView('4xx');
- }
- $this->template->statusCode = $exception->getCode();
- $this->accessLog($exception);
- } elseif ($exception instanceof Kdyby\Redis\SessionHandlerException) {
- if (!$this->getHttpResponse()->isSent()) {
- $this->getHttpResponse()->setCode(429); // 429 Too Many Requests
- }
- $this->setView('429');
- $this->accessLog($exception, 429);
- } else {
- $this->setView('500'); // load template 500.latte
- Debugger::log($exception, Debugger::ERROR); // and log exception
- }
- if ($this->isAjax()) { // AJAX request? Just note this error in payload.
- $this->payload->error = TRUE;
- $this->terminate();
- }
- }
- protected function accessLog(\Exception $exception, $code = NULL)
- {
- $projectRoot = dirname(dirname(dirname(__DIR__))) . '/';
- // log to access.log
- $this->logger->addInfo(
- sprintf('HTTP code %d: %s in %s:%d at %s',
- $code ?: $exception->getCode(),
- $exception->getMessage(),
- str_replace($projectRoot, '', $exception->getFile()),
- $exception->getLine(),
- (string) $this->getHttpRequest()->getUrl()
- ),
- [
- 'referer' => (string) $this->getHttpRequest()->getReferer(),
- 'channel' => 'access',
- ]
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment