/** * @param Exception $exception */ public function logException(Exception $exception) { $formatter = new CM_ExceptionHandling_Formatter_Plain_Log(); try { if ($exception instanceof CM_Exception) { $log = $exception->getLog(); $metaInfo = $exception->getMetaInfo(); } else { $log = new CM_Paging_Log_Error(); $metaInfo = null; } $log->add($formatter->formatException($exception), $metaInfo); } catch (Exception $loggerException) { $logEntry = '[' . date('d.m.Y - H:i:s', time()) . ']' . PHP_EOL; $logEntry .= '### Cannot log error: ' . PHP_EOL; $logEntry .= $formatter->formatException($loggerException); $logEntry .= '### Original Exception: ' . PHP_EOL; $logEntry .= $formatter->formatException($exception) . PHP_EOL; $logFile = $this->_getLogFile(); $logFile->ensureParentDirectory(); $logFile->append($logEntry); } }
/** * @param callable $regularCode * @param callable $errorCode * @return mixed * @throws CM_Exception */ protected function _runWithCatching(Closure $regularCode, Closure $errorCode) { try { return $regularCode(); } catch (CM_Exception $ex) { $exceptionClass = get_class($ex); $config = self::_getConfig(); $exceptionsToCatch = $config->exceptionsToCatch; $catchPublicExceptions = !empty($config->catchPublicExceptions); $catchException = false; $errorOptions = []; if (array_key_exists($exceptionClass, $exceptionsToCatch)) { $catchException = true; $errorOptions = $exceptionsToCatch[$exceptionClass]; if (isset($errorOptions['log'])) { $formatter = new CM_ExceptionHandling_Formatter_Plain_Log(); /** @var CM_Paging_Log_Abstract $log */ $log = new $errorOptions['log'](); $log->add($formatter->formatException($ex), $ex->getMetaInfo()); } } if ($catchPublicExceptions && $ex->isPublic()) { $catchException = true; } if ($catchException) { return $errorCode($ex, $errorOptions); } throw $ex; } }
/** * @param callable $regularCode * @param callable $errorCode * @return mixed * @throws CM_Exception */ protected function _runWithCatching(Closure $regularCode, Closure $errorCode) { try { return $regularCode(); } catch (CM_Exception $ex) { $config = self::_getConfig(); $exceptionsToCatch = $config->exceptionsToCatch; $catchPublicExceptions = !empty($config->catchPublicExceptions); $errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use($ex) { return is_a($ex, $exceptionClass); }); $catchException = null !== $errorOptions; if ($catchException) { if (isset($errorOptions['log'])) { $formatter = new CM_ExceptionHandling_Formatter_Plain_Log(); /** @var CM_Paging_Log_Abstract $log */ $log = new $errorOptions['log'](); $log->add($formatter->formatException($ex), $ex->getMetaInfo()); } } if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) { $errorOptions = []; $catchException = true; } if ($catchException) { return $errorCode($ex, $errorOptions); } throw $ex; } }