PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/kohana/exception.php

https://bitbucket.org/Ahineya/trn_dev
PHP | 214 lines | 106 code | 31 blank | 77 comment | 11 complexity | c62261296ac39b5cd6615d53b4720351 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct access');
  2. /**
  3. * Kohana exception class. Translates exceptions using the [I18n] class.
  4. *
  5. * @package Kohana
  6. * @category Exceptions
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2011 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_Kohana_Exception extends Exception {
  12. /**
  13. * @var array PHP error code => human readable name
  14. */
  15. public static $php_errors = array(
  16. E_ERROR => 'Fatal Error',
  17. E_USER_ERROR => 'User Error',
  18. E_PARSE => 'Parse Error',
  19. E_WARNING => 'Warning',
  20. E_USER_WARNING => 'User Warning',
  21. E_STRICT => 'Strict',
  22. E_NOTICE => 'Notice',
  23. E_RECOVERABLE_ERROR => 'Recoverable Error',
  24. );
  25. /**
  26. * @var string error rendering view
  27. */
  28. public static $error_view = 'kohana/error';
  29. /**
  30. * @var string error view content type
  31. */
  32. public static $error_view_content_type = 'text/html';
  33. /**
  34. * Creates a new translated exception.
  35. *
  36. * throw new Kohana_Exception('Something went terrible wrong, :user',
  37. * array(':user' => $user));
  38. *
  39. * @param string error message
  40. * @param array translation variables
  41. * @param integer|string the exception code
  42. * @return void
  43. */
  44. public function __construct($message, array $variables = NULL, $code = 0)
  45. {
  46. if (defined('E_DEPRECATED'))
  47. {
  48. // E_DEPRECATED only exists in PHP >= 5.3.0
  49. Kohana_Exception::$php_errors[E_DEPRECATED] = 'Deprecated';
  50. }
  51. // Set the message
  52. $message = __($message, $variables);
  53. // Pass the message and integer code to the parent
  54. parent::__construct($message, (int) $code);
  55. // Save the unmodified code
  56. // @link http://bugs.php.net/39615
  57. $this->code = $code;
  58. }
  59. /**
  60. * Magic object-to-string method.
  61. *
  62. * echo $exception;
  63. *
  64. * @uses Kohana_Exception::text
  65. * @return string
  66. */
  67. public function __toString()
  68. {
  69. return Kohana_Exception::text($this);
  70. }
  71. /**
  72. * Inline exception handler, displays the error message, source of the
  73. * exception, and the stack trace of the error.
  74. *
  75. * @uses Kohana_Exception::text
  76. * @param object exception object
  77. * @return boolean
  78. */
  79. public static function handler(Exception $e)
  80. {
  81. try
  82. {
  83. // Get the exception information
  84. $type = get_class($e);
  85. $code = $e->getCode();
  86. $message = $e->getMessage();
  87. $file = $e->getFile();
  88. $line = $e->getLine();
  89. // Get the exception backtrace
  90. $trace = $e->getTrace();
  91. if ($e instanceof ErrorException)
  92. {
  93. if (isset(Kohana_Exception::$php_errors[$code]))
  94. {
  95. // Use the human-readable error name
  96. $code = Kohana_Exception::$php_errors[$code];
  97. }
  98. if (version_compare(PHP_VERSION, '5.3', '<'))
  99. {
  100. // Workaround for a bug in ErrorException::getTrace() that exists in
  101. // all PHP 5.2 versions. @see http://bugs.php.net/bug.php?id=45895
  102. for ($i = count($trace) - 1; $i > 0; --$i)
  103. {
  104. if (isset($trace[$i - 1]['args']))
  105. {
  106. // Re-position the args
  107. $trace[$i]['args'] = $trace[$i - 1]['args'];
  108. // Remove the args
  109. unset($trace[$i - 1]['args']);
  110. }
  111. }
  112. }
  113. }
  114. // Create a text version of the exception
  115. $error = Kohana_Exception::text($e);
  116. if (is_object(Kohana::$log))
  117. {
  118. // Add this exception to the log
  119. Kohana::$log->add(Log::ERROR, $error);
  120. $strace = Kohana_Exception::text($e)."\n--\n" . $e->getTraceAsString();
  121. Kohana::$log->add(Log::STRACE, $strace);
  122. // Make sure the logs are written
  123. Kohana::$log->write();
  124. }
  125. if (Kohana::$is_cli)
  126. {
  127. // Just display the text of the exception
  128. echo "\n{$error}\n";
  129. exit(1);
  130. }
  131. if ( ! headers_sent())
  132. {
  133. // Make sure the proper http header is sent
  134. $http_header_status = ($e instanceof HTTP_Exception) ? $code : 500;
  135. header('Content-Type: '.Kohana_Exception::$error_view_content_type.'; charset='.Kohana::$charset, TRUE, $http_header_status);
  136. }
  137. if (Request::$current !== NULL AND Request::current()->is_ajax() === TRUE)
  138. {
  139. // Just display the text of the exception
  140. echo "\n{$error}\n";
  141. exit(1);
  142. }
  143. // Start an output buffer
  144. ob_start();
  145. // Include the exception HTML
  146. if ($view_file = Kohana::find_file('views', Kohana_Exception::$error_view))
  147. {
  148. include $view_file;
  149. }
  150. else
  151. {
  152. throw new Kohana_Exception('Error view file does not exist: views/:file', array(
  153. ':file' => Kohana_Exception::$error_view,
  154. ));
  155. }
  156. // Display the contents of the output buffer
  157. echo ob_get_clean();
  158. exit(1);
  159. }
  160. catch (Exception $e)
  161. {
  162. // Clean the output buffer if one exists
  163. ob_get_level() and ob_clean();
  164. // Display the exception text
  165. echo Kohana_Exception::text($e), "\n";
  166. // Exit with an error status
  167. exit(1);
  168. }
  169. }
  170. /**
  171. * Get a single line of text representing the exception:
  172. *
  173. * Error [ Code ]: Message ~ File [ Line ]
  174. *
  175. * @param object Exception
  176. * @return string
  177. */
  178. public static function text(Exception $e)
  179. {
  180. return sprintf('%s [ %s ]: %s ~ %s [ %d ]',
  181. get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine());
  182. }
  183. } // End Kohana_Exception