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