diff options
Diffstat (limited to '')
23 files changed, 484 insertions, 0 deletions
diff --git a/library/Icinga/Exception/AlreadyExistsException.php b/library/Icinga/Exception/AlreadyExistsException.php new file mode 100644 index 0000000..d70c58f --- /dev/null +++ b/library/Icinga/Exception/AlreadyExistsException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Exception thrown if something to add already exists + */ +class AlreadyExistsException extends IcingaException +{ +} diff --git a/library/Icinga/Exception/AuthenticationException.php b/library/Icinga/Exception/AuthenticationException.php new file mode 100644 index 0000000..50910b8 --- /dev/null +++ b/library/Icinga/Exception/AuthenticationException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Exception thrown if an error occurs during authentication + */ +class AuthenticationException extends IcingaException +{ +} diff --git a/library/Icinga/Exception/ConfigurationError.php b/library/Icinga/Exception/ConfigurationError.php new file mode 100644 index 0000000..e66ec46 --- /dev/null +++ b/library/Icinga/Exception/ConfigurationError.php @@ -0,0 +1,12 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Class ConfigurationError + * @package Icinga\Exception + */ +class ConfigurationError extends IcingaException +{ +} diff --git a/library/Icinga/Exception/Http/BaseHttpException.php b/library/Icinga/Exception/Http/BaseHttpException.php new file mode 100644 index 0000000..cad41c6 --- /dev/null +++ b/library/Icinga/Exception/Http/BaseHttpException.php @@ -0,0 +1,73 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Http; + +use Icinga\Exception\IcingaException; + +/** + * Base class for HTTP exceptions + */ +class BaseHttpException extends IcingaException implements HttpExceptionInterface +{ + /** + * This exception's HTTP status code + * + * @var int + */ + protected $statusCode; + + /** + * This exception's HTTP response headers + * + * @var array + */ + protected $headers; + + /** + * Return this exception's HTTP status code + * + * @return int + */ + public function getStatusCode() + { + return $this->statusCode; + } + + /** + * Set this exception's HTTP response headers + * + * @param array $headers + * + * @return $this + */ + public function setHeaders(array $headers) + { + $this->headers = $headers; + return $this; + } + + /** + * Set/Add a HTTP response header + * + * @param string $name + * @param string $value + * + * @return $this + */ + public function setHeader($name, $value) + { + $this->headers[$name] = $value; + return $this; + } + + /** + * Return this exception's HTTP response headers + * + * @return array An array where each key is a header name and the value its value + */ + public function getHeaders() + { + return $this->headers ?: array(); + } +} diff --git a/library/Icinga/Exception/Http/HttpBadRequestException.php b/library/Icinga/Exception/Http/HttpBadRequestException.php new file mode 100644 index 0000000..004eabd --- /dev/null +++ b/library/Icinga/Exception/Http/HttpBadRequestException.php @@ -0,0 +1,12 @@ +<?php +/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Http; + +/** + * Exception thrown for sending a HTTP 400 response w/ a custom message + */ +class HttpBadRequestException extends BaseHttpException +{ + protected $statusCode = 400; +} diff --git a/library/Icinga/Exception/Http/HttpException.php b/library/Icinga/Exception/Http/HttpException.php new file mode 100644 index 0000000..cd6b543 --- /dev/null +++ b/library/Icinga/Exception/Http/HttpException.php @@ -0,0 +1,25 @@ +<?php +/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Http; + +class HttpException extends BaseHttpException +{ + /** + * Create a new HttpException + * + * @param int $statusCode HTTP status code + * @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. + */ + public function __construct($statusCode, $message) + { + $this->statusCode = (int) $statusCode; + + $args = func_get_args(); + array_shift($args); + call_user_func_array('parent::__construct', $args); + } +} diff --git a/library/Icinga/Exception/Http/HttpExceptionInterface.php b/library/Icinga/Exception/Http/HttpExceptionInterface.php new file mode 100644 index 0000000..c5e0cc7 --- /dev/null +++ b/library/Icinga/Exception/Http/HttpExceptionInterface.php @@ -0,0 +1,21 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Http; + +interface HttpExceptionInterface +{ + /** + * Return this exception's HTTP status code + * + * @return int + */ + public function getStatusCode(); + + /** + * Return this exception's HTTP response headers + * + * @return array An array where each key is a header name and the value its value + */ + public function getHeaders(); +} diff --git a/library/Icinga/Exception/Http/HttpMethodNotAllowedException.php b/library/Icinga/Exception/Http/HttpMethodNotAllowedException.php new file mode 100644 index 0000000..4e40b6a --- /dev/null +++ b/library/Icinga/Exception/Http/HttpMethodNotAllowedException.php @@ -0,0 +1,36 @@ +<?php +/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Http; + +/** + * Exception thrown if the HTTP method is not allowed + */ +class HttpMethodNotAllowedException extends BaseHttpException +{ + protected $statusCode = 405; + + /** + * Get the allowed HTTP methods + * + * @return string + */ + public function getAllowedMethods() + { + $headers = $this->getHeaders(); + return isset($headers['Allow']) ? $headers['Allow'] : null; + } + + /** + * Set the allowed HTTP methods + * + * @param string $allowedMethods + * + * @return $this + */ + public function setAllowedMethods($allowedMethods) + { + $this->setHeader('Allow', (string) $allowedMethods); + return $this; + } +} diff --git a/library/Icinga/Exception/Http/HttpNotFoundException.php b/library/Icinga/Exception/Http/HttpNotFoundException.php new file mode 100644 index 0000000..eb91d63 --- /dev/null +++ b/library/Icinga/Exception/Http/HttpNotFoundException.php @@ -0,0 +1,12 @@ +<?php +/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Http; + +/** + * Exception thrown for sending a HTTP 404 response w/ a custom message + */ +class HttpNotFoundException extends BaseHttpException +{ + protected $statusCode = 404; +} diff --git a/library/Icinga/Exception/IcingaException.php b/library/Icinga/Exception/IcingaException.php new file mode 100644 index 0000000..a87ea4a --- /dev/null +++ b/library/Icinga/Exception/IcingaException.php @@ -0,0 +1,107 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +use Exception; +use ReflectionClass; + +class IcingaException extends Exception +{ + /** + * Create a new exception + * + * @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. + */ + public function __construct($message) + { + $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); + } + + /** + * 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); + } + + /** + * 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() + ); + } + + /** + * 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) { + $trace[] = isset($frame['file']) + ? "#{$index} {$frame['file']}({$frame['line']}): " + : "#{$index} [internal function]: "; + + if (isset($frame['class'])) { + $trace[] = $frame['class']; + } + + if (isset($frame['type'])) { + $trace[] = $frame['type']; + } + + $trace[] = "{$frame['function']}("; + + if (isset($frame['args'])) { + $args = array(); + foreach ($frame['args'] as $arg) { + $type = gettype($arg); + $args[] = $type === 'object' ? 'Object(' . get_class($arg) . ')' : ucfirst($type); + } + + $trace[] = implode(', ', $args); + } + $trace[] = ")\n"; + } + + $trace[] = '#' . ($index + 1) . ' {main}'; + + return implode($trace); + } +} diff --git a/library/Icinga/Exception/InvalidPropertyException.php b/library/Icinga/Exception/InvalidPropertyException.php new file mode 100644 index 0000000..e7bcf32 --- /dev/null +++ b/library/Icinga/Exception/InvalidPropertyException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Exception thrown if a property does not exist + */ +class InvalidPropertyException extends IcingaException +{ +} diff --git a/library/Icinga/Exception/Json/JsonDecodeException.php b/library/Icinga/Exception/Json/JsonDecodeException.php new file mode 100644 index 0000000..978eb30 --- /dev/null +++ b/library/Icinga/Exception/Json/JsonDecodeException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Json; + +/** + * Exception thrown by {@link \Icinga\Util\Json::decode()} on failure + */ +class JsonDecodeException extends JsonException +{ +} diff --git a/library/Icinga/Exception/Json/JsonEncodeException.php b/library/Icinga/Exception/Json/JsonEncodeException.php new file mode 100644 index 0000000..0bcc6c0 --- /dev/null +++ b/library/Icinga/Exception/Json/JsonEncodeException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Json; + +/** + * Exception thrown by {@link \Icinga\Util\Json::encode()} on failure + */ +class JsonEncodeException extends JsonException +{ +} diff --git a/library/Icinga/Exception/Json/JsonException.php b/library/Icinga/Exception/Json/JsonException.php new file mode 100644 index 0000000..2ca3605 --- /dev/null +++ b/library/Icinga/Exception/Json/JsonException.php @@ -0,0 +1,13 @@ +<?php +/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception\Json; + +use Icinga\Exception\IcingaException; + +/** + * Exception thrown by {@link \Icinga\Util\Json} on failure + */ +abstract class JsonException extends IcingaException +{ +} diff --git a/library/Icinga/Exception/MissingParameterException.php b/library/Icinga/Exception/MissingParameterException.php new file mode 100644 index 0000000..a8bd78d --- /dev/null +++ b/library/Icinga/Exception/MissingParameterException.php @@ -0,0 +1,40 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Exception thrown if a mandatory parameter was not given + */ +class MissingParameterException extends IcingaException +{ + /** + * Name of the missing parameter + * + * @var string + */ + protected $parameter; + + /** + * Get the name of the missing parameter + * + * @return string + */ + public function getParameter() + { + return $this->parameter; + } + + /** + * Set the name of the missing parameter + * + * @param string $name + * + * @return $this + */ + public function setParameter($name) + { + $this->parameter = (string) $name; + return $this; + } +} diff --git a/library/Icinga/Exception/NotFoundError.php b/library/Icinga/Exception/NotFoundError.php new file mode 100644 index 0000000..74e6941 --- /dev/null +++ b/library/Icinga/Exception/NotFoundError.php @@ -0,0 +1,8 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +class NotFoundError extends IcingaException +{ +} diff --git a/library/Icinga/Exception/NotImplementedError.php b/library/Icinga/Exception/NotImplementedError.php new file mode 100644 index 0000000..395b4b2 --- /dev/null +++ b/library/Icinga/Exception/NotImplementedError.php @@ -0,0 +1,12 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Class NotImplementedError + * @package Icinga\Exception + */ +class NotImplementedError extends IcingaException +{ +} diff --git a/library/Icinga/Exception/NotReadableError.php b/library/Icinga/Exception/NotReadableError.php new file mode 100644 index 0000000..6bf2b3c --- /dev/null +++ b/library/Icinga/Exception/NotReadableError.php @@ -0,0 +1,8 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +class NotReadableError extends IcingaException +{ +} diff --git a/library/Icinga/Exception/NotWritableError.php b/library/Icinga/Exception/NotWritableError.php new file mode 100644 index 0000000..efe1fbb --- /dev/null +++ b/library/Icinga/Exception/NotWritableError.php @@ -0,0 +1,8 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +class NotWritableError extends IcingaException +{ +} diff --git a/library/Icinga/Exception/ProgrammingError.php b/library/Icinga/Exception/ProgrammingError.php new file mode 100644 index 0000000..02d4b47 --- /dev/null +++ b/library/Icinga/Exception/ProgrammingError.php @@ -0,0 +1,12 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Class ProgrammingError + * @package Icinga\Exception + */ +class ProgrammingError extends IcingaException +{ +} diff --git a/library/Icinga/Exception/QueryException.php b/library/Icinga/Exception/QueryException.php new file mode 100644 index 0000000..9344b86 --- /dev/null +++ b/library/Icinga/Exception/QueryException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Exception thrown if a query encountered an error + */ +class QueryException extends IcingaException +{ +} diff --git a/library/Icinga/Exception/StatementException.php b/library/Icinga/Exception/StatementException.php new file mode 100644 index 0000000..7501c86 --- /dev/null +++ b/library/Icinga/Exception/StatementException.php @@ -0,0 +1,8 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +class StatementException extends IcingaException +{ +} diff --git a/library/Icinga/Exception/SystemPermissionException.php b/library/Icinga/Exception/SystemPermissionException.php new file mode 100644 index 0000000..5651169 --- /dev/null +++ b/library/Icinga/Exception/SystemPermissionException.php @@ -0,0 +1,11 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Exception; + +/** + * Handle problems according to file system permissions + */ +class SystemPermissionException extends IcingaException +{ +} |