-
Notifications
You must be signed in to change notification settings - Fork 6
/
Exception.php
executable file
·132 lines (122 loc) · 2.76 KB
/
Exception.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
*
* Generic exception class.
*
* @category Solar
*
* @package Solar
*
* @author Paul M. Jones <pmjones@solarphp.com>
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
* @version $Id$
*
*/
class Solar_Exception extends Exception
{
/**
*
* User-defined information array.
*
* @var array
*
*/
protected $_info = array();
/**
*
* Class where the exception originated.
*
* @var array
*
*/
protected $_class;
/**
*
* Constructor.
*
* @param array $config Configuration value overrides, if any.
* for 'class', 'code', 'text', and 'info'.
*
*/
public function __construct($config = null)
{
$default = array(
'class' => '',
'code' => '',
'text' => '',
'info' => array(),
);
$config = array_merge($default, (array) $config);
parent::__construct($config['text']);
$this->code = $config['code'];
$this->_class = $config['class'];
$this->_info = (array) $config['info'];
}
/**
*
* Returnes the exception as a string.
*
* @return void
*
*/
public function __toString()
{
$class_code = $this->_class . "::" . $this->code;
// basic string
$str = "exception '" . get_class($this) . "'\n"
. "class::code '$class_code' \n"
. "with message '" . $this->message . "' \n"
. "information " . var_export($this->_info, true) . " \n"
. "Stack trace:\n"
. " " . str_replace("\n", "\n ", $this->getTraceAsString());
// at the CLI, repeat the message so it shows up as the last line
// of output, not the trace.
if (PHP_SAPI == 'cli') {
$str .= "\n\n{$this->message}";
}
// done
return $str;
}
/**
*
* Returns user-defined information.
*
* @param string $key A particular info key to return; if empty, returns
* all info.
*
* @return array
*
*/
final public function getInfo($key = null)
{
if (empty($key)) {
return $this->_info;
} else {
return $this->_info[$key];
}
}
/**
*
* Returns the class name that threw the exception.
*
* @return string
*
*/
final public function getClass()
{
return $this->_class;
}
/**
*
* Returns the class name and code together.
*
* @return string
*
*/
final public function getClassCode()
{
return $this->_class . '::' . $this->code;
}
}