2010-09-02 17:49:00 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ErrorHandler for Console Shells
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 20:59:49 +09:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-09-02 17:49:00 -04:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-09-02 17:49:00 -04:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 20:59:49 +09:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-09-02 17:49:00 -04:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-10-03 01:40:12 -04:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2013-05-31 00:11:14 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-09-02 17:49:00 -04:00
|
|
|
*/
|
2013-05-31 00:11:14 +02:00
|
|
|
|
2010-12-07 01:26:10 -04:30
|
|
|
App::uses('ErrorHandler', 'Error');
|
|
|
|
App::uses('ConsoleOutput', 'Console');
|
2010-12-08 23:15:18 -04:30
|
|
|
App::uses('CakeLog', 'Log');
|
2010-09-02 17:49:00 -04:00
|
|
|
|
|
|
|
/**
|
2011-08-15 23:55:08 -04:00
|
|
|
* Error Handler for Cake console. Does simple printing of the
|
2010-09-02 17:49:00 -04:00
|
|
|
* exception that occurred and the stack trace of the error.
|
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Console
|
2010-09-02 17:49:00 -04:00
|
|
|
*/
|
2011-10-02 23:16:41 -04:00
|
|
|
class ConsoleErrorHandler {
|
2010-09-02 17:49:00 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard error stream.
|
|
|
|
*
|
2011-07-28 22:03:44 -04:00
|
|
|
* @var ConsoleOutput
|
2010-09-02 17:49:00 -04:00
|
|
|
*/
|
2010-11-14 22:47:35 -05:00
|
|
|
public static $stderr;
|
2010-09-02 17:49:00 -04:00
|
|
|
|
|
|
|
/**
|
2010-11-14 22:47:35 -05:00
|
|
|
* Get the stderr object for the console error handling.
|
2010-09-02 17:49:00 -04:00
|
|
|
*
|
2011-07-28 22:03:44 -04:00
|
|
|
* @return ConsoleOutput
|
2010-09-02 17:49:00 -04:00
|
|
|
*/
|
2010-11-14 22:47:35 -05:00
|
|
|
public static function getStderr() {
|
2015-07-21 10:22:53 +02:00
|
|
|
if (empty(static::$stderr)) {
|
|
|
|
static::$stderr = new ConsoleOutput('php://stderr');
|
2010-11-14 22:47:35 -05:00
|
|
|
}
|
2015-07-21 10:22:53 +02:00
|
|
|
return static::$stderr;
|
2010-09-02 17:49:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-30 22:11:03 +02:00
|
|
|
* Handle an exception in the console environment. Prints a message to stderr.
|
2010-09-02 17:49:00 -04:00
|
|
|
*
|
2010-12-21 23:11:23 -05:00
|
|
|
* @param Exception $exception The exception to handle
|
2010-09-02 17:49:00 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-02 23:16:41 -04:00
|
|
|
public function handleException(Exception $exception) {
|
2015-07-21 10:22:53 +02:00
|
|
|
$stderr = static::getStderr();
|
2011-04-25 21:17:59 +02:00
|
|
|
$stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
|
2011-08-15 23:55:08 -04:00
|
|
|
$exception->getMessage(),
|
2010-11-14 22:47:35 -05:00
|
|
|
$exception->getTraceAsString()
|
|
|
|
));
|
2014-06-26 13:12:28 +02:00
|
|
|
$code = $exception->getCode();
|
2014-06-26 14:22:34 +02:00
|
|
|
$code = ($code && is_int($code)) ? $code : 1;
|
2014-06-26 13:12:28 +02:00
|
|
|
return $this->_stop($code);
|
2010-09-02 18:02:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-25 21:46:52 -04:00
|
|
|
* Handle errors in the console environment. Writes errors to stderr,
|
|
|
|
* and logs messages if Configure::read('debug') is 0.
|
2010-09-02 18:02:58 -04:00
|
|
|
*
|
2014-07-03 15:36:42 +02:00
|
|
|
* @param int $code Error code
|
2010-12-21 23:11:23 -05:00
|
|
|
* @param string $description Description of the error.
|
|
|
|
* @param string $file The file the error occurred in.
|
2014-07-03 15:36:42 +02:00
|
|
|
* @param int $line The line the error occurred on.
|
2010-12-21 23:11:23 -05:00
|
|
|
* @param array $context The backtrace of the error.
|
2010-09-02 18:02:58 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-02 23:16:41 -04:00
|
|
|
public function handleError($code, $description, $file = null, $line = null, $context = null) {
|
2010-12-02 01:44:31 -02:00
|
|
|
if (error_reporting() === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-21 10:22:53 +02:00
|
|
|
$stderr = static::getStderr();
|
2011-10-02 23:16:41 -04:00
|
|
|
list($name, $log) = ErrorHandler::mapErrorCode($code);
|
2011-03-19 18:32:35 +01:00
|
|
|
$message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
|
|
|
|
$stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));
|
2010-11-25 23:16:27 -05:00
|
|
|
|
2012-09-14 18:42:25 +01:00
|
|
|
if (!Configure::read('debug')) {
|
2010-11-25 23:16:27 -05:00
|
|
|
CakeLog::write($log, $message);
|
|
|
|
}
|
2012-09-14 14:17:07 +02:00
|
|
|
|
|
|
|
if ($log === LOG_ERR) {
|
2013-07-01 14:15:54 +02:00
|
|
|
return $this->_stop(1);
|
2012-09-14 14:17:07 +02:00
|
|
|
}
|
2010-09-02 18:02:58 -04:00
|
|
|
}
|
2010-09-05 11:22:39 -04:00
|
|
|
|
2011-10-02 23:16:41 -04:00
|
|
|
/**
|
|
|
|
* Wrapper for exit(), used for testing.
|
|
|
|
*
|
2014-07-03 15:36:42 +02:00
|
|
|
* @param int $code The exit code.
|
2013-07-05 17:19:22 +02:00
|
|
|
* @return void
|
2011-10-02 23:16:41 -04:00
|
|
|
*/
|
|
|
|
protected function _stop($code = 0) {
|
|
|
|
exit($code);
|
|
|
|
}
|
|
|
|
|
2010-09-02 17:49:00 -04:00
|
|
|
}
|