/
ConsoleErrorHandler.php
103 lines (94 loc) · 2.61 KB
/
ConsoleErrorHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console;
use Cake\Core\Configure;
use Cake\Error\FatalErrorException;
use Cake\Error\BaseErrorHandler;
use Cake\Error\ErrorHandler;
/**
* Error Handler for Cake console. Does simple printing of the
* exception that occurred and the stack trace of the error.
*/
class ConsoleErrorHandler extends BaseErrorHandler {
/**
* Standard error stream.
*
* @var ConsoleOutput
*/
protected $_stderr;
/**
* Options for this instance.
*
* @var array
*/
protected $_options;
/**
* Constructor
*
* @param array $options Options for the error handler.
*/
public function __construct($options = []) {
if (empty($options['stderr'])) {
$options['stderr'] = new ConsoleOutput('php://stderr');
}
$this->_stderr = $options['stderr'];
$this->_options = $options;
}
/**
* Handle a exception in the console environment. Prints a message to stderr.
*
* @param Exception $exception The exception to handle
* @return integer Exit code from exception caught.
*/
public function handleException(\Exception $exception) {
$errorName = __d('cake_console', 'Error:');
if ($exception instanceof FatalErrorException) {
$errorName = __d('cake_console', 'Fatal Error:');
}
$message = sprintf(
"<error>%s</error> %s in [%s, line %s]\n%s",
$errorName,
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$exception->getTraceAsString()
);
$this->_stderr->write($message);
return $exception->getCode() ?: 1;
}
protected function _displayException($exception) {
}
/**
* Prints an error to stderr.
*
* Template method of BaseErrorHandler.
*
* @param array $error An array of error data.
* @param boolean $debug Whether or not the app is in debug mode.
* @return void
*/
protected function _displayError($error, $debug) {
$message = __d('cake_console', '%s in [%s, line %s]',
$error['description'],
$error['file'],
$error['line']
);
$message = __d('cake_console', "<error>%s Error:</error> %s\n",
$error['error'],
$message
);
$this->_stderr->write($message);
}
}