-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exception.php
263 lines (228 loc) · 7.98 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
START LICENSE AND COPYRIGHT
This file is part of ZfExtended library
Copyright (c) 2013 - 2021 Marc Mittag; MittagQI - Quality Informatics; All rights reserved.
Contact: http://www.MittagQI.com/ / service (ATT) MittagQI.com
This file may be used under the terms of the GNU LESSER GENERAL PUBLIC LICENSE version 3
as published by the Free Software Foundation and appearing in the file lgpl3-license.txt
included in the packaging of this file. Please review the following information
to ensure the GNU LESSER GENERAL PUBLIC LICENSE version 3.0 requirements will be met:
https://www.gnu.org/licenses/lgpl-3.0.txt
@copyright Marc Mittag, MittagQI - Quality Informatics
@author MittagQI - Quality Informatics
@license GNU LESSER GENERAL PUBLIC LICENSE version 3
https://www.gnu.org/licenses/lgpl-3.0.txt
END LICENSE AND COPYRIGHT
*/
class ZfExtended_Exception extends Zend_Exception
{
/***
* Additional data field which, if set will be returned in the exception json response
*/
public const EXTRA_DATA_FIELD = 'extraData';
/**
* the error/event level of this exception (how "important" that error is)
* By default all Exceptions are of level error
* @var integer
*/
protected $level = ZfExtended_Logger::LEVEL_ERROR;
/**
* @var ZfExtended_Zendoverwrites_Translate
*/
protected $_translate;
/**
* internal errors store
* @var array
*/
protected $errors = [];
/**
* internal domain store (domain in the sense of area)
* @var string
*/
protected $domain = 'core';
/**
* FIXME should be replaced with a loglevel based way
* Flag if logging for this exception is enabled / disabled
* @var boolean
*/
protected $loggingEnabled = true;
/**
* @var string
*/
protected $defaultMessage = '';
/**
* @var boolean
*/
protected $defaultMessageTranslate = false;
/**
* @var integer
*/
protected $defaultCode = 0;
/**
* Construct the exception
*
* @param string $msg (Message gets translated by ZfExtended_Exception)
* @param int $code
* @param null $domain optional, defaults to core. Can be the plugin name, or another system identifier
*/
public function __construct($msg = '', $code = 0, Throwable $previous = null, $domain = null)
{
if ((int) $code === 0) {
$code = $this->defaultCode;
}
if ($msg == '') {
$this->setMessage($this->defaultMessage, $this->defaultMessageTranslate);
} else {
$this->setMessage($msg);
}
if (empty($domain)) {
$domain = $this->domain;
}
$this->setDomain($domain);
parent::__construct($this->message, (int) $code, $previous);
}
/**
* sets the internal exception message
* @param string $msg
* @param bool $translate optional, set to true if the message should be translated
*/
public function setMessage($msg, $translate = false)
{
if ($translate) {
$this->_translate = ZfExtended_Zendoverwrites_Translate::getInstance();
$msg = $this->_translate->_($msg);
}
$this->message = $msg;
//FIXME add a flag here, to find out if it is the default message or a custom message.
// bring this info (custom message or default message) to the frontend, so that we can react there better
// Also we should differ in the excption messages for the different log levels.
// A Exception should contain content for each loglevel. The message ported to the frontend for example should not contain debug data.
}
/**
* stores the given errors internally
* @deprecated refactor the called exception instance to errorCodeException and use the extra/data container there
* //FIXME searc for usages and refactor it
*/
public function setErrors(array $errors)
{
$this->errors = $errors;
}
/**
* return the internally stored errors
* @deprecated refactor the called exception instance to errorCodeException and use the extra/data container there
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* stores the domain of the exception (plugin name, etc), defaults to core
* @param string $domain
*/
public function setDomain($domain)
{
$this->domain = $domain;
}
/**
* return the internally stored domain
* @return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* return the error/event level of this exception (how "important" that error is)
* @return integer
*/
public function getLevel()
{
return $this->level;
}
/**
* Adds the additional not translated error information to the log output
* {@inheritDoc}
* @see Zend_Exception::__toString()
*/
public function __toString()
{
$errors = '';
if (! empty($this->errors)) {
$errors = "\n\n Additional error data: " . mb_substr(print_r($this->errors, 1), 1024 * 5);
}
return parent::__toString() . $errors;
}
/**
* FIXME should be replaced with a loglevel based way
*
* returns true if logging should be done for this exception
* We can force to enable the logging even if the exception was coded not to log by setting this in the config:
* runtimeOptions.logging.default.delete.index.ZfExtended_BadMethodCallException = true
* where default is the module, delete the controller and index the action to be considered
* Module, Controller and Action are each optional, so the config syntax would be:
* runtimeOptions.logging.[default.[delete.[index.]]]EXCEPTION_CLASS_NAME
* the module part can be overwritten by BaseIndex::setModule, so caution in configuration here.
* @return boolean
*/
public function isLoggingEnabled()
{
$config = Zend_Registry::get('config');
/**
* Startpoint in the Config tree
*/
$logConf = $config->runtimeOptions->logging;
/**
* the names of needed parts (module, action, etc)
*/
$exception = get_class($this);
$mod = Zend_Registry::get('module'); //warning this can be changed be BaseIndex::setModule
$f = Zend_Registry::get('frontController');
/* @var $f Zend_Controller_Front */
$contr = $f->getRequest()->getControllerName();
$action = $f->getRequest()->getActionName();
/**
* all possible config paths are defined in this array
* @var array
*/
$pathsToCheck = [
[$exception],
[$mod, $exception],
[$mod, $contr, $exception],
[$mod, $contr, $action, $exception],
];
/**
* @return boolean if a config was found, NULL if nothing was configured for the path
*/
$checkPath = function ($start, $path) use (&$checkPath) {
if (! is_null($start) && ! ($start instanceof Zend_Config)) {
throw new Exception('start is not NULL and not instanceof Zend_Config');
}
$part = array_shift($path);
if (is_null($start) || ! isset($start->$part)) {
return null;
}
if ($start->$part instanceof Zend_Config) {
return $checkPath($start->$part, $path);
}
return (bool) $start->$part;
};
//walk over each $pathToCheck and look ap the config for it
foreach ($pathsToCheck as $path) {
$res = $checkPath($logConf, $path);
if (! is_null($res)) {
return $res;
}
}
return $this->loggingEnabled;
}
/**
* enables / disables the logging for this exception
* @param bool $enabled
*/
public function setLogging($enabled = true)
{
$this->loggingEnabled = $enabled;
}
}