Send an exception to Sentry, but only if the "logException" rendering option is TRUE

During compiletime there might be missing dependencies, so we need additional safeguards to
not cause errors.

@param object $exception \Exception or \Throwable
547142
protected function sendExceptionToSentry($exception)
    {
        if (!Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) {
            return;
        }

        $options = $this->resolveCustomRenderingOptions($exception);
        if (isset($options['logException']) && $options['logException']) {
            try {
                /** @var ErrorHandler $errorHandler */
                $errorHandler = Bootstrap::$staticObjectManager->get(ErrorHandler::class);
                if ($errorHandler !== null) {
                    $errorHandler->handleException($exception);
                }
            } catch (\Exception $exception) {
                // Quick'n dirty workaround to catch exception with the error handler is called during compile time
            }
        }
    }
874332
public function wrapAndHandleException($exception)
    {
        if ($exception instanceof Error) {
            $exception = new PHP7ErrorException($exception);
        }
        $this->handleException($exception);
    }
860763
protected function _displayException($exception)
    {
        $rendererClassName = App::className($this->_options['exceptionRenderer'], 'Error');
        try {
            if (!$rendererClassName) {
                throw new Exception("$rendererClassName is an invalid class.");
            }
            /** @var \Cake\Error\ExceptionRendererInterface $renderer */
            $renderer = new $rendererClassName($exception);
            $response = $renderer->render();
            $this->_clearOutput();
            $this->_sendResponse($response);
        } catch (Throwable $exception) {
            $this->_logInternalError($exception);
        } catch (Exception $exception) {
            $this->_logInternalError($exception);
        }
    }
183013
private function logException(Exception $exception): void
    {
        if ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() < 500) {
            return;
        }

        $this->logger->critical(
            sprintf(
                'Uncaught PHP Exception %s: "%s" at %s line %s',
                get_class($exception),
                $exception->getMessage(),
                $exception->getFile(),
                $exception->getLine()
            ),
            ['exception' => $exception]
        );
    }
900745
protected function logException(\Exception $exception)
    {
        try {
            $this->logger->error($exception->getMessage() , array('exception' => $exception));
        } catch (\Exception $exception) {
            trigger_error("Error logging Monitor exception: ".$exception->getMessage());
        }
    }
1000774
public function logException($exception)
    {
        if (!$this->inExceptionHandler)
        {
            Yii::$app->bugsnag->notifyException($exception);
        }

        try
        {
            Yii::error("Caught exception " . get_class($exception) . ": " . (string)$exception, BugsnagComponent::IGNORED_LOG_CATEGORY);
        }
        catch (\Exception $e) {}
    }
627836
public function handleException($exception)
    {
        // Ignore if the error is suppressed by using the shut-up operator @
        if (error_reporting() === 0) {
            return;
        }

        $this->renderingOptions = $this->resolveCustomRenderingOptions($exception);

        if ($this->throwableStorage instanceof ThrowableStorageInterface && isset($this->renderingOptions['logException']) && $this->renderingOptions['logException']) {
            $message = $this->throwableStorage->logThrowable($exception);
            $this->logger->critical($message);
        }

        switch (PHP_SAPI) {
            case 'cli':
                $this->echoExceptionCli($exception);
                break;
            default:
                $this->echoExceptionWeb($exception);
        }
    }
846452
public function logException( Exception $exception,
                                  $priority = Logger::CRIT )
    {
        /* @var $logger \Zork\Log\LoggerManager */
        $logger = $this->getServiceLocator()
                       ->get( 'Zork\Log\LoggerManager' );

        if ( $logger->hasLogger( 'exception' ) )
        {
            $logger->getLogger( 'exception' )
                   ->log( $priority,
                          '<pre>' . $exception . PHP_EOL . '</pre>' . PHP_EOL );
        }
    }
213421
protected function logException(\Exception $exception, $message)
    {
        if (null !== $this->logger) {
            if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
                $this->logger->critical($message, array('exception' => $exception));
            } else {
                $this->logger->error($message, array('exception' => $exception));
            }
        }
    }
186956
public function handleException($exception)
  {
    // Disable error capturing to avoid recursive errors while handling exceptions.
    $this->unregister();

    try
    {
      $this->clearOutput();
      $this->renderException($exception);

      exit(-1);
    }
    catch (\Exception $e)
    {
      // Another exception could be thrown while displaying the exception.
      $message = "An Error occurred while handling another error:\n";
      $message .= (string)$e;
      $message .= "\nPrevious exception:\n";
      $message .= (string)$exception;
      if ($this->myDebug)
      {
        $this->echoMessage($message);
      }
      else
      {
        $this->echoMessage('An internal server error occurred.');
      }
      $message .= "\n\$_SERVER = ".print_r($_SERVER, true);
      error_log($message);

      exit(1);
    }
  }
1218992
protected function echoExceptionCli(\Throwable $exception)
    {
        $response = new CliResponse();

        $exceptionMessage = $this->renderSingleExceptionCli($exception);
        $exceptionMessage = $this->renderNestedExceptonsCli($exception, $exceptionMessage);

        if ($exception instanceof FlowException) {
            $exceptionMessage .= PHP_EOL . 'Open <b>Data/Logs/Exceptions/' . $exception->getReferenceCode() . '.txt</b> for a full stack trace.' . PHP_EOL;
        }

        $response->setContent($exceptionMessage);
        $response->send();
        exit(1);
    }
639434
static public function exceptionHandler($exception)
    {
        $msg = $exception->getMessage()
                . " Line "
                . $exception->getLine()
                . ", "
                . $exception->getFile();
        self::$logger->debug($msg);
    }
334636
protected function logException(\Exception $exception): void
    {
        if (!$this->logger) {
            return;
        }

        $message = sprintf(
            'Exception thrown when handling an exception (%s: %s at %s line %s)',
            get_class($exception),
            $exception->getMessage(),
            $exception->getFile(),
            $exception->getLine()
        );

        $this->logger->log($this->level, $message, [
            'exception' => $exception,
        ]);
    }
190849
public function handleException(\Exception $exception) {
    // get error level
    $logFunction = 'error';
    if ($exception instanceof ApplicationException) {
      $logFunction = $exception->getError()->getLevel() == ApplicationError::LEVEL_WARNING ?
              'warn' : 'error';
    }
    self::$logger->$logFunction($exception);

    try {
      if (ObjectFactory::getInstance('configuration') != null) {
        // rollback current transaction
        $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
        $persistenceFacade->getTransaction()->rollback();

        // redirect to failure action
        if ($this->request) {
          $error = ApplicationError::fromException($exception);
          $this->request->addError($error);
          $this->response->addError($error);
          $this->request->setAction('failure');
          $this->response->setAction('failure');
          $this->response->setStatus($error->getStatusCode());
          ObjectFactory::getInstance('actionMapper')->processAction($this->request, $this->response);
          return;
        }
      }
      throw $exception;
    }
    catch (\Exception $ex) {
      self::$logger->error($ex->getMessage()."\n".$ex->getTraceAsString());
    }
  }
724181
public function handleExceptions($exception)
    {
        $this->displayError($exception);
        $this->sendErrorNotification($exception);

        if(isset($this->ApplicationContext) && ($this->ApplicationContext->isOneOffRedeploy() || $this->redeployOnError))
            $this->ApplicationContext->clearContextFiles();
    }
294126
public function handleException($exception)
    {
        //Log the error
        Injector::inst()->get(LoggerInterface::class)->debug($exception->getMessage());
        $request = Injector::inst()->get(HTTPRequest::class);
        $request->getSession()->set('notice', true);
        $request->getSession()->set('noticeType', 'bad');
        $message = $exception->getMessage();
        if ($exception->hasResponse()) {
            $response = json_decode($exception->getResponse()->getBody(), true);
            if (isset($response['error']) && isset($response['error']['message'])) {
                $message = $response['error']['message'];
            }
        }
        $request->getSession()->set('noticeText', $message);
    }
931581
public function handler(Throwable $exception): void
	{
		try
		{
			// Empty output buffers

			$this->clearOutputBuffers();

			// Loop through the exception handlers

			foreach($this->handlers as $handler)
			{
				if($exception instanceof $handler['exceptionType'])
				{
					if($this->handleException($handler['handler'], $exception) !== null)
					{
						break;
					}
				}
			}

			// Log exception

			if($this->shouldExceptionBeLogged($exception))
			{
				$this->getLogger()->error($exception->getMessage(), ['exception' => $exception]);
			}
		}
		catch(Throwable $e)
		{
			// Empty output buffers

			$this->clearOutputBuffers();

			// One of the exception handlers failed so we'll just show the user a generic error screen

			echo $e->getMessage() . ' on line [ ' . $e->getLine() . ' ] in [ ' . $e->getFile() . ' ]' . PHP_EOL;
		}

		exit(1);
	}