ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ilErrorHandling Class Reference

Error Handling & global info handling. More...

+ Collaboration diagram for ilErrorHandling:

Public Member Functions

 __construct ()
 
 getHandler ()
 Get a handler for an error or exception. More...
 
 raiseError (string $message, ?int $code=null)
 
 getMessage ()
 
 setMessage (string $a_message)
 
 appendMessage (string $a_message)
 
 handlePreWhoops (int $level, string $message, string $file, int $line)
 Parameter types according to PHP doc: set_error_handler. More...
 

Data Fields

int $FATAL = 1
 Error level 1: exit application immedietly. More...
 
int $WARNING = 2
 Error level 2: show warning page. More...
 
int $MESSAGE = 3
 Error level 3: show message in recent page. More...
 

Protected Member Functions

 initWhoopsHandlers ()
 Initialize Error and Exception Handlers. More...
 
 getRuntime ()
 
 getWhoops ()
 
 isDevmodeActive ()
 
 defaultHandler ()
 
 devmodeHandler ()
 
 addEditorSupport (PrettyPageHandler $handler)
 
 applyEditorPathTranslations (string &$file, array $pathTranslations)
 
 parseEditorPathTranslation (string $pathTranslationConfig)
 

Protected Attributes

RunInterface $whoops
 
string $message
 

Static Protected Attributes

static bool $whoops_handlers_registered = false
 Are the whoops error handlers already registered? More...
 

Private Member Functions

 errorHandler (string $message, int $code, array $backtrace)
 

Private Attributes

const array SENSTIVE_PARAMETER_NAMES
 

Detailed Description

Error Handling & global info handling.

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Sascha Hofmann shofm.nosp@m.ann@.nosp@m.datab.nosp@m.ay.d.nosp@m.e
Richard Klees richa.nosp@m.rd.k.nosp@m.lees@.nosp@m.conc.nosp@m.epts-.nosp@m.and-.nosp@m.train.nosp@m.ing..nosp@m.de
Stefan Hecken stefa.nosp@m.n.he.nosp@m.cken@.nosp@m.conc.nosp@m.epts-.nosp@m.and-.nosp@m.train.nosp@m.ing..nosp@m.de
Todo:

when an error occured and clicking the back button to return to previous page the referer-var in session is deleted -> server error

This class is a candidate for a singleton. initHandlers could only be called once per process anyways, as it checks for static $handlers_registered.

Definition at line 37 of file class.ilErrorHandling.php.

Constructor & Destructor Documentation

◆ __construct()

ilErrorHandling::__construct ( )

Definition at line 65 of file class.ilErrorHandling.php.

66 {
67 $this->FATAL = 1;
68 $this->WARNING = 2;
69 $this->MESSAGE = 3;
70
71 $this->initWhoopsHandlers();
72
73 // somehow we need to get rid of the whoops error handler
74 restore_error_handler();
75 set_error_handler([$this, 'handlePreWhoops']);
76 }
initWhoopsHandlers()
Initialize Error and Exception Handlers.

References initWhoopsHandlers().

+ Here is the call graph for this function:

Member Function Documentation

◆ addEditorSupport()

ilErrorHandling::addEditorSupport ( PrettyPageHandler  $handler)
protected

Definition at line 313 of file class.ilErrorHandling.php.

313 : void
314 {
315 $editorUrl = defined('ERROR_EDITOR_URL') ? ERROR_EDITOR_URL : '';
316 if (!is_string($editorUrl) || $editorUrl === '') {
317 return;
318 }
319
320 $pathTranslationConfig = defined('ERROR_EDITOR_PATH_TRANSLATIONS') ? ERROR_EDITOR_PATH_TRANSLATIONS : '';
321
322 $pathTranslations = $this->parseEditorPathTranslation($pathTranslationConfig);
323
324 $handler->setEditor(function ($file, $line) use ($editorUrl, $pathTranslations) {
325 $this->applyEditorPathTranslations($file, $pathTranslations);
326
327 return str_ireplace(
328 ['[FILE]', '[LINE]'],
329 [$file, $line],
330 $editorUrl
331 );
332 });
333 }
parseEditorPathTranslation(string $pathTranslationConfig)
applyEditorPathTranslations(string &$file, array $pathTranslations)
$handler
Definition: oai.php:31

References $handler.

◆ appendMessage()

ilErrorHandling::appendMessage ( string  $a_message)

Definition at line 205 of file class.ilErrorHandling.php.

205 : void
206 {
207 if ($this->getMessage()) {
208 $this->message .= '<br /> ';
209 }
210 $this->message .= $a_message;
211 }

◆ applyEditorPathTranslations()

ilErrorHandling::applyEditorPathTranslations ( string &  $file,
array  $pathTranslations 
)
protected

Definition at line 335 of file class.ilErrorHandling.php.

335 : void
336 {
337 foreach ($pathTranslations as $from => $to) {
338 $file = preg_replace('@' . $from . '@', $to, $file);
339 }
340 }

◆ defaultHandler()

ilErrorHandling::defaultHandler ( )
protected

Definition at line 228 of file class.ilErrorHandling.php.

228 : HandlerInterface
229 {
230 return new CallbackHandler(function ($exception, Inspector $inspector, Run $run) {
231 global $DIC;
232
234
235 $message = 'Sorry, an error occured.';
236 if ($DIC->isDependencyAvailable('language')) {
237 $DIC->language()->loadLanguageModule('logging');
238 $message = $DIC->language()->txt('error_sry_error');
239 }
240
241 if (!empty($logger->folder())) {
242 $session_id = substr(session_id(), 0, 5);
243 $r = new \Random\Randomizer();
244 $err_num = $r->getInt(1, 9999);
245 $file_name = $session_id . '_' . $err_num;
246
247 $lwriter = new ilLoggingErrorFileStorage($inspector, $logger->folder(), $file_name);
248 $lwriter = $lwriter->withExclusionList(self::SENSTIVE_PARAMETER_NAMES);
249 $lwriter->write();
250
251 if ($DIC->isDependencyAvailable('language')) {
252 $message = sprintf($DIC->language()->txt('log_error_message'), $file_name);
253 if ($logger->mail()) {
254 $message .= ' ' . sprintf(
255 $DIC->language()->txt('log_error_message_send_mail'),
256 $logger->mail(),
257 $file_name,
258 $logger->mail()
259 );
260 }
261 } else {
262 $message = 'Sorry, an error occured. A logfile has been created which can be identified via the code "' . $file_name . '"';
263 if ($logger->mail()) {
264 $message .= ' ' . 'Please send a mail to <a href="mailto:' . $logger->mail() . '?subject=code: ' . $file_name . '">' . $logger->mail() . '</a>';
265 }
266 }
267 }
268
269 if ($DIC->isDependencyAvailable('ui') && isset($DIC['tpl']) && $DIC->isDependencyAvailable('ctrl')) {
270 $DIC->ui()->mainTemplate()->setOnScreenMessage('failure', $message, true);
271 $DIC->ctrl()->redirectToURL('error.php');
272 } else {
273 ilSession::set('failure', $message);
274 header('Location: error.php');
275 exit;
276 }
277 });
278 }
static set(string $a_var, $a_val)
Set a value.
exit
global $DIC
Definition: shib_login.php:26

References $DIC, exit, ilLoggingErrorSettings\getInstance(), ILIAS\UI\examples\Symbol\Glyph\Header\header(), and ilSession\set().

Referenced by getHandler().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ devmodeHandler()

ilErrorHandling::devmodeHandler ( )
protected

Definition at line 280 of file class.ilErrorHandling.php.

280 : HandlerInterface
281 {
282 global $ilLog;
283
284 switch (ERROR_HANDLER) {
285 case 'TESTING':
286 return (new ilTestingHandler())->withExclusionList(self::SENSTIVE_PARAMETER_NAMES);
287
288 case 'PLAIN_TEXT':
289 return (new ilPlainTextHandler())->withExclusionList(self::SENSTIVE_PARAMETER_NAMES);
290
291 case 'PRETTY_PAGE':
292 // fallthrough
293 default:
294 if ((!defined('ERROR_HANDLER') || ERROR_HANDLER !== 'PRETTY_PAGE') && $ilLog) {
295 $ilLog->write(
296 "Unknown or undefined error handler '" . ERROR_HANDLER . "'. " .
297 'Falling back to PrettyPageHandler.'
298 );
299 }
300
301 $prettyPageHandler = new PrettyPageHandler();
302
303 $this->addEditorSupport($prettyPageHandler);
304
305 foreach (self::SENSTIVE_PARAMETER_NAMES as $param) {
306 $prettyPageHandler->blacklist('_POST', $param);
307 }
308
309 return $prettyPageHandler;
310 }
311 }
addEditorSupport(PrettyPageHandler $handler)
A Whoops error handler that prints the same content as the PrettyPageHandler but as plain text.
A Whoops error handler for testing.
$param
Definition: xapitoken.php:44

References $param.

Referenced by getHandler().

+ Here is the caller graph for this function:

◆ errorHandler()

ilErrorHandling::errorHandler ( string  $message,
int  $code,
array  $backtrace 
)
private
Parameters
list<array{"function"string, "line"?: int, "file"?: string, "class"?: class-string,"type"?: "->"|"::", "object"?: object, "args"?: list<mixed>}> $backtrace

Definition at line 138 of file class.ilErrorHandling.php.

138 : void
139 {
140 global $log;
141
142 $session_failure = ilSession::get('failure');
143 if ($session_failure) {
144 $m = 'Fatal Error: Called raise error two times.<br>' .
145 'First error: ' . $session_failure . '<br>' .
146 'Last Error:' . $message;
147 $log->write($m);
148 ilSession::clear('failure');
149 die($m);
150 }
151
152 if ($log instanceof ilLogger) {
153 $log->write($message);
154 }
155 if ($code === $this->FATAL) {
156 throw new RuntimeException(stripslashes($message));
157 }
158
159 if ($code === $this->WARNING) {
160 ilSession::set('failure', $message);
161 if (defined('ILIAS_MODULE')) {
162 ilUtil::redirect('../error.php');
163 } else {
164 ilUtil::redirect('error.php');
165 }
166 }
167 $updir = '';
168 if ($code === $this->MESSAGE) {
169 ilSession::set('failure', $message);
170 // save post vars to session in case of error
171 $_SESSION['error_post_vars'] = $_POST;
172
173 if (empty($_SESSION['referer'])) {
174 $dirname = dirname($_SERVER['PHP_SELF']);
175 $ilurl = parse_url(ilUtil::_getHttpPath());
176
177 $subdir = '';
178 if (is_array($ilurl) && array_key_exists('path', $ilurl) && $ilurl['path'] !== '') {
179 $subdir = substr(strstr($dirname, (string) $ilurl['path']), strlen((string) $ilurl['path']));
180 $updir = '';
181 }
182 if ($subdir) {
183 $num_subdirs = substr_count($subdir, '/');
184
185 for ($i = 1; $i <= $num_subdirs; $i++) {
186 $updir .= '../';
187 }
188 }
189 ilUtil::redirect($updir . 'index.php');
190 }
191 ilUtil::redirect($_SESSION['referer']);
192 }
193 }
Component logger with individual log levels by component id.
static get(string $a_var)
static clear(string $a_var)
static _getHttpPath()
static redirect(string $a_script)
$_POST['cmd']
Definition: lti.php:27
$log
Definition: ltiresult.php:34
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26

References $_POST, $_SERVER, $log, ilUtil\_getHttpPath(), ilSession\clear(), ilSession\get(), ilUtil\redirect(), and ilSession\set().

+ Here is the call graph for this function:

◆ getHandler()

ilErrorHandling::getHandler ( )

Get a handler for an error or exception.

Uses Whoops Pretty Page Handler in DEVMODE and the legacy ILIAS-Error handlers otherwise.

Definition at line 105 of file class.ilErrorHandling.php.

105 : HandlerInterface
106 {
108 strcasecmp($_SERVER['REQUEST_METHOD'] ?? '', 'post') === 0) {
109 return new ilSoapExceptionHandler();
110 }
111
112 // TODO: There might be more specific execution contexts (WebDAV, REST, etc.) that need specific error handling.
113
114 if ($this->isDevmodeActive()) {
115 return $this->devmodeHandler();
116 }
117
118 return $this->defaultHandler();
119 }
static getType()
Get context type.
Definition: ilContext.php:165
const CONTEXT_SOAP
Definition: ilContext.php:34

References $_SERVER, ilContext\CONTEXT_SOAP, defaultHandler(), devmodeHandler(), ilContext\getType(), and isDevmodeActive().

+ Here is the call graph for this function:

◆ getMessage()

ilErrorHandling::getMessage ( )

Definition at line 195 of file class.ilErrorHandling.php.

195 : string
196 {
197 return $this->message;
198 }

Referenced by ILIAS\UI\Implementation\Component\Input\Field\FormInput\withInput(), and ILIAS\UI\Implementation\Component\Input\Input\withInput().

+ Here is the caller graph for this function:

◆ getRuntime()

ilErrorHandling::getRuntime ( )
protected

Definition at line 213 of file class.ilErrorHandling.php.

213 : ilRuntime
214 {
215 return ilRuntime::getInstance();
216 }
static getInstance()

References ilRuntime\getInstance().

Referenced by initWhoopsHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getWhoops()

ilErrorHandling::getWhoops ( )
protected

Definition at line 218 of file class.ilErrorHandling.php.

218 : RunInterface
219 {
220 return new Run();
221 }

Referenced by initWhoopsHandlers().

+ Here is the caller graph for this function:

◆ handlePreWhoops()

ilErrorHandling::handlePreWhoops ( int  $level,
string  $message,
string  $file,
int  $line 
)

Parameter types according to PHP doc: set_error_handler.

Exceptions

Whoops\Exception\ErrorException

Definition at line 394 of file class.ilErrorHandling.php.

394 : bool
395 {
396 global $ilLog;
397
398 if ($level & error_reporting()) {
399 if (!$this->isDevmodeActive()) {
400 // log E_USER_NOTICE, E_STRICT, E_DEPRECATED, E_USER_DEPRECATED only
401 if ($level >= E_USER_NOTICE) {
402 if ($ilLog) {
403 $severity = Whoops\Util\Misc::translateErrorCode($level);
404 $ilLog->write("\n\n" . $severity . ' - ' . $message . "\n" . $file . ' - line ' . $line . "\n");
405 }
406
407 return true;
408 }
409 }
410
411 if ($level === E_USER_DEPRECATED) {
412 if ($ilLog) {
413 $severity = Whoops\Util\Misc::translateErrorCode($level);
414 $ilLog->write("\n\n" . $severity . ' - ' . $message . "\n" . $file . ' - line ' . $line . "\n");
415 }
416
417 return true;
418 }
419
420 if ($this->whoops instanceof RunInterface) {
421 return $this->whoops->handleError($level, $message, $file, $line);
422 }
423 }
424
425 return true;
426 }

◆ initWhoopsHandlers()

ilErrorHandling::initWhoopsHandlers ( )
protected

Initialize Error and Exception Handlers.

Initializes Whoops, a logging handler and a delegate handler for the late initialization of an appropriate error handler.

Definition at line 83 of file class.ilErrorHandling.php.

83 : void
84 {
85 if (self::$whoops_handlers_registered) {
86 // Only register whoops error handlers once.
87 return;
88 }
89
90 $runtime = $this->getRuntime();
91 $this->whoops = $this->getWhoops();
92 $this->whoops->pushHandler(new ilDelegatingHandler($this, self::SENSTIVE_PARAMETER_NAMES));
93 if ($runtime->shouldLogErrors()) {
94 $this->whoops->pushHandler($this->loggingHandler());
95 }
96 $this->whoops->register();
97
98 self::$whoops_handlers_registered = true;
99 }
A Whoops error handler that delegates calls on it self to another handler that is created only in the...

References getRuntime(), and getWhoops().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isDevmodeActive()

ilErrorHandling::isDevmodeActive ( )
protected

Definition at line 223 of file class.ilErrorHandling.php.

223 : bool
224 {
225 return defined('DEVMODE') && (int) DEVMODE === 1;
226 }

References ILIAS\Repository\int().

Referenced by getHandler().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseEditorPathTranslation()

ilErrorHandling::parseEditorPathTranslation ( string  $pathTranslationConfig)
protected

Definition at line 342 of file class.ilErrorHandling.php.

342 : array
343 {
344 $pathTranslations = [];
345
346 $mappings = explode('|', $pathTranslationConfig);
347 foreach ($mappings as $mapping) {
348 $parts = explode(',', $mapping);
349 if (count($parts) === 2) {
350 $pathTranslations[trim($parts[0])] = trim($parts[1]);
351 }
352 }
353
354 return $pathTranslations;
355 }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61

References $parts.

◆ raiseError()

ilErrorHandling::raiseError ( string  $message,
?int  $code = null 
)

Definition at line 121 of file class.ilErrorHandling.php.

124 : void {
125 $backtrace = debug_backtrace();
126 if (isset($backtrace[0]['object'])) {
127 unset($backtrace[0]['object']);
128 }
129
130 // see bug 18499 (some calls to raiseError do not pass a code, which leads to security issues, if these calls
131 // are done due to permission checks)
132 $this->errorHandler($message, $code ?? $this->WARNING, $backtrace);
133 }
errorHandler(string $message, int $code, array $backtrace)

◆ setMessage()

ilErrorHandling::setMessage ( string  $a_message)

Definition at line 200 of file class.ilErrorHandling.php.

200 : void
201 {
202 $this->message = $a_message;
203 }

Field Documentation

◆ $FATAL

int ilErrorHandling::$FATAL = 1

Error level 1: exit application immedietly.

Definition at line 59 of file class.ilErrorHandling.php.

◆ $message

string ilErrorHandling::$message
protected

Definition at line 57 of file class.ilErrorHandling.php.

◆ $MESSAGE

int ilErrorHandling::$MESSAGE = 3

Error level 3: show message in recent page.

Definition at line 63 of file class.ilErrorHandling.php.

◆ $WARNING

int ilErrorHandling::$WARNING = 2

Error level 2: show warning page.

Definition at line 61 of file class.ilErrorHandling.php.

◆ $whoops

RunInterface ilErrorHandling::$whoops
protected

Definition at line 56 of file class.ilErrorHandling.php.

◆ $whoops_handlers_registered

bool ilErrorHandling::$whoops_handlers_registered = false
staticprotected

Are the whoops error handlers already registered?

Definition at line 54 of file class.ilErrorHandling.php.

◆ SENSTIVE_PARAMETER_NAMES

const array ilErrorHandling::SENSTIVE_PARAMETER_NAMES
private
Initial value:
= [
'password',
'passwd',
'passwd_retype',
'current_password',
'usr_password',
'usr_password_retype',
'new_password',
'new_password_retype',
]

Definition at line 40 of file class.ilErrorHandling.php.


The documentation for this class was generated from the following file: