diff options
Diffstat (limited to 'library/Icinga/Exception/Http')
6 files changed, 179 insertions, 0 deletions
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; +} |