PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/work/libraries/src/Exception/ExceptionHandler.php

https://bitbucket.org/programmerlab/ourteam.co.in
PHP | 171 lines | 107 code | 28 blank | 36 comment | 13 complexity | 051786c3ee2aa13153e5a10ce6c7e804 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, 0BSD, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * Joomla! Content Management System
  4. *
  5. * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. namespace Joomla\CMS\Exception;
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Displays the custom error page when an uncaught exception occurs.
  12. *
  13. * @since 3.0
  14. */
  15. class ExceptionHandler
  16. {
  17. /**
  18. * Render the error page based on an exception.
  19. *
  20. * @param \Exception|\Throwable $error An Exception or Throwable (PHP 7+) object for which to render the error page.
  21. *
  22. * @return void
  23. *
  24. * @since 3.0
  25. */
  26. public static function render($error)
  27. {
  28. $expectedClass = PHP_MAJOR_VERSION >= 7 ? '\Throwable' : '\Exception';
  29. $isException = $error instanceof $expectedClass;
  30. // In PHP 5, the $error object should be an instance of \Exception; PHP 7 should be a Throwable implementation
  31. if ($isException)
  32. {
  33. try
  34. {
  35. // Try to log the error, but don't let the logging cause a fatal error
  36. try
  37. {
  38. \JLog::add(
  39. sprintf(
  40. 'Uncaught %1$s of type %2$s thrown. Stack trace: %3$s',
  41. $expectedClass,
  42. get_class($error),
  43. $error->getTraceAsString()
  44. ),
  45. \JLog::CRITICAL,
  46. 'error'
  47. );
  48. }
  49. catch (\Throwable $e)
  50. {
  51. // Logging failed, don't make a stink about it though
  52. }
  53. catch (\Exception $e)
  54. {
  55. // Logging failed, don't make a stink about it though
  56. }
  57. $app = \JFactory::getApplication();
  58. // If site is offline and it's a 404 error, just go to index (to see offline message, instead of 404)
  59. if ($error->getCode() == '404' && $app->get('offline') == 1)
  60. {
  61. $app->redirect('index.php');
  62. }
  63. $attributes = array(
  64. 'charset' => 'utf-8',
  65. 'lineend' => 'unix',
  66. 'tab' => "\t",
  67. 'language' => 'en-GB',
  68. 'direction' => 'ltr',
  69. );
  70. // If there is a \JLanguage instance in \JFactory then let's pull the language and direction from its metadata
  71. if (\JFactory::$language)
  72. {
  73. $attributes['language'] = \JFactory::getLanguage()->getTag();
  74. $attributes['direction'] = \JFactory::getLanguage()->isRtl() ? 'rtl' : 'ltr';
  75. }
  76. $document = \JDocument::getInstance('error', $attributes);
  77. if (!$document)
  78. {
  79. // We're probably in an CLI environment
  80. jexit($error->getMessage());
  81. }
  82. // Get the current template from the application
  83. $template = $app->getTemplate();
  84. // Push the error object into the document
  85. $document->setError($error);
  86. if (ob_get_contents())
  87. {
  88. ob_end_clean();
  89. }
  90. $document->setTitle(\JText::_('ERROR') . ': ' . $error->getCode());
  91. $data = $document->render(
  92. false,
  93. array(
  94. 'template' => $template,
  95. 'directory' => JPATH_THEMES,
  96. 'debug' => JDEBUG,
  97. )
  98. );
  99. // Do not allow cache
  100. $app->allowCache(false);
  101. // If nothing was rendered, just use the message from the Exception
  102. if (empty($data))
  103. {
  104. $data = $error->getMessage();
  105. }
  106. $app->setBody($data);
  107. echo $app->toString();
  108. $app->close(0);
  109. // This return is needed to ensure the test suite does not trigger the non-Exception handling below
  110. return;
  111. }
  112. catch (\Throwable $e)
  113. {
  114. // Pass the error down
  115. }
  116. catch (\Exception $e)
  117. {
  118. // Pass the error down
  119. }
  120. }
  121. // This isn't an Exception, we can't handle it.
  122. if (!headers_sent())
  123. {
  124. header('HTTP/1.1 500 Internal Server Error');
  125. }
  126. $message = 'Error displaying the error page';
  127. if ($isException)
  128. {
  129. // Make sure we do not display sensitive data in production environments
  130. if (ini_get('display_errors'))
  131. {
  132. $message .= ': ';
  133. if (isset($e))
  134. {
  135. $message .= $e->getMessage() . ': ';
  136. }
  137. $message .= $error->getMessage();
  138. }
  139. }
  140. echo $message;
  141. jexit(1);
  142. }
  143. }