2014-08-22 10:23:12 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-08-22 10:23:12 +02:00
|
|
|
|
|
|
|
namespace Icinga\Exception;
|
|
|
|
|
|
|
|
use Exception;
|
2015-07-27 16:25:41 +02:00
|
|
|
use ReflectionClass;
|
2014-08-22 10:23:12 +02:00
|
|
|
|
|
|
|
class IcingaException extends Exception
|
|
|
|
{
|
|
|
|
/**
|
2015-07-28 10:45:00 +02:00
|
|
|
* Create a new exception
|
2014-08-22 10:23:12 +02:00
|
|
|
*
|
2015-07-28 10:45:00 +02:00
|
|
|
* @param string $message Exception message or exception format string
|
|
|
|
* @param mixed ...$arg Format string argument
|
|
|
|
*
|
|
|
|
* If there is at least one exception, the last one will be used for exception chaining.
|
2014-08-22 10:23:12 +02:00
|
|
|
*/
|
2015-07-28 10:43:17 +02:00
|
|
|
public function __construct($message)
|
2014-08-22 10:23:12 +02:00
|
|
|
{
|
|
|
|
$args = array_slice(func_get_args(), 1);
|
|
|
|
$exc = null;
|
|
|
|
foreach ($args as &$arg) {
|
|
|
|
if ($arg instanceof Exception) {
|
|
|
|
$exc = $arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::__construct(vsprintf($message, $args), 0, $exc);
|
|
|
|
}
|
2015-07-27 16:25:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the exception from an array of arguments
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function create(array $args)
|
|
|
|
{
|
|
|
|
$e = new ReflectionClass(get_called_class());
|
|
|
|
return $e->newInstanceArgs($args);
|
|
|
|
}
|
2015-07-28 13:46:32 +02:00
|
|
|
|
2015-07-24 16:19:20 +02:00
|
|
|
/**
|
|
|
|
* Return the given exception formatted as one-liner
|
|
|
|
*
|
|
|
|
* The format used is: %class% in %path%:%line% with message: %message%
|
|
|
|
*
|
|
|
|
* @param Exception $exception
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function describe(Exception $exception)
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'%s in %s:%d with message: %s',
|
|
|
|
get_class($exception),
|
|
|
|
$exception->getFile(),
|
|
|
|
$exception->getLine(),
|
|
|
|
$exception->getMessage()
|
|
|
|
);
|
|
|
|
}
|
2018-01-22 10:28:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the same as {@link Exception::getTraceAsString()} for the given exception,
|
|
|
|
* but show only the types of scalar arguments
|
|
|
|
*
|
|
|
|
* @param Exception $exception
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getConfidentialTraceAsString(Exception $exception)
|
|
|
|
{
|
|
|
|
$trace = array();
|
|
|
|
|
|
|
|
foreach ($exception->getTrace() as $index => $frame) {
|
2018-01-25 14:20:41 +01:00
|
|
|
$trace[] = isset($frame['file'])
|
|
|
|
? "#{$index} {$frame['file']}({$frame['line']}): "
|
|
|
|
: "#{$index} [internal function]: ";
|
2018-01-22 10:28:47 +01:00
|
|
|
|
|
|
|
if (isset($frame['class'])) {
|
|
|
|
$trace[] = $frame['class'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($frame['type'])) {
|
|
|
|
$trace[] = $frame['type'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$trace[] = "{$frame['function']}(";
|
|
|
|
|
2018-02-13 12:46:06 +01:00
|
|
|
if (isset($frame['args'])) {
|
|
|
|
$args = array();
|
|
|
|
foreach ($frame['args'] as $arg) {
|
|
|
|
$type = gettype($arg);
|
|
|
|
$args[] = $type === 'object' ? 'Object(' . get_class($arg) . ')' : ucfirst($type);
|
|
|
|
}
|
2018-01-22 10:28:47 +01:00
|
|
|
|
2018-02-13 12:46:06 +01:00
|
|
|
$trace[] = implode(', ', $args);
|
|
|
|
}
|
2018-01-22 10:28:47 +01:00
|
|
|
$trace[] = ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$trace[] = '#' . ($index + 1) . ' {main}';
|
|
|
|
|
|
|
|
return implode($trace);
|
|
|
|
}
|
2014-08-22 10:23:12 +02:00
|
|
|
}
|