PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ekiwookie/juss
PHP | 215 lines | 106 code | 31 blank | 78 comment | 11 complexity | 1c3034e8a4149dbe666217d64ddcf0ba 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-2012 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 $message error message
  40. * @param array $variables translation variables
  41. * @param integer|string $code 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 Exception $e
  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
  101. // exists in all PHP 5.2 versions.
  102. // @link http://bugs.php.net/45895
  103. for ($i = count($trace) - 1; $i > 0; --$i)
  104. {
  105. if (isset($trace[$i - 1]['args']))
  106. {
  107. // Re-position the args
  108. $trace[$i]['args'] = $trace[$i - 1]['args'];
  109. // Remove the args
  110. unset($trace[$i - 1]['args']);
  111. }
  112. }
  113. }
  114. }
  115. // Create a text version of the exception
  116. $error = Kohana_Exception::text($e);
  117. if (is_object(Kohana::$log))
  118. {
  119. // Add this exception to the log
  120. Kohana::$log->add(Log::ERROR, $error);
  121. $strace = Kohana_Exception::text($e)."\n--\n" . $e->getTraceAsString();
  122. Kohana::$log->add(Log::STRACE, $strace);
  123. // Make sure the logs are written
  124. Kohana::$log->write();
  125. }
  126. if (Kohana::$is_cli)
  127. {
  128. // Just display the text of the exception
  129. echo "\n{$error}\n";
  130. exit(1);
  131. }
  132. if ( ! headers_sent())
  133. {
  134. // Make sure the proper http header is sent
  135. $http_header_status = ($e instanceof HTTP_Exception) ? $code : 500;
  136. header('Content-Type: '.Kohana_Exception::$error_view_content_type.'; charset='.Kohana::$charset, TRUE, $http_header_status);
  137. }
  138. if (Request::$current !== NULL AND Request::current()->is_ajax() === TRUE)
  139. {
  140. // Just display the text of the exception
  141. echo "\n{$error}\n";
  142. exit(1);
  143. }
  144. // Start an output buffer
  145. ob_start();
  146. // Include the exception HTML
  147. if ($view_file = Kohana::find_file('views', Kohana_Exception::$error_view))
  148. {
  149. include $view_file;
  150. }
  151. else
  152. {
  153. throw new Kohana_Exception('Error view file does not exist: views/:file', array(
  154. ':file' => Kohana_Exception::$error_view,
  155. ));
  156. }
  157. // Display the contents of the output buffer
  158. echo ob_get_clean();
  159. exit(1);
  160. }
  161. catch (Exception $e)
  162. {
  163. // Clean the output buffer if one exists
  164. ob_get_level() and ob_clean();
  165. // Display the exception text
  166. echo Kohana_Exception::text($e), "\n";
  167. // Exit with an error status
  168. exit(1);
  169. }
  170. }
  171. /**
  172. * Get a single line of text representing the exception:
  173. *
  174. * Error [ Code ]: Message ~ File [ Line ]
  175. *
  176. * @param Exception $e
  177. * @return string
  178. */
  179. public static function text(Exception $e)
  180. {
  181. return sprintf('%s [ %s ]: %s ~ %s [ %d ]',
  182. get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine());
  183. }
  184. } // End Kohana_Exception