From b41bf77ec8187b6c7bdc5229510032662901e416 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 23 Nov 2017 23:40:54 -0500 Subject: [PATCH 1/4] Add HTTPException class files --- src/Network/HTTPException.php | 23 +++++++++++++++++++ .../HTTPException/BadGatewayException.php | 10 ++++++++ .../HTTPException/BadRequestException.php | 10 ++++++++ .../HTTPException/ConflictException.php | 10 ++++++++ .../ExpectationFailedException.php | 10 ++++++++ .../HTTPException/ForbiddenException.php | 10 ++++++++ .../HTTPException/GatewayTimeoutException.php | 10 ++++++++ src/Network/HTTPException/GoneException.php | 10 ++++++++ .../HTTPException/ImATeapotException.php | 11 +++++++++ .../InternalServerErrorException.php | 10 ++++++++ .../HTTPException/LenghtRequiredException.php | 10 ++++++++ .../MethodNotAllowedException.php | 10 ++++++++ .../HTTPException/NonAcceptableException.php | 10 ++++++++ .../HTTPException/NotFoundException.php | 9 ++++++++ .../HTTPException/NotImplementedException.php | 10 ++++++++ .../PreconditionFailedException.php | 10 ++++++++ .../ServiceUnavaiableException.php | 10 ++++++++ .../TooManyRequestsException.php | 10 ++++++++ .../HTTPException/UnauthorizedException.php | 10 ++++++++ .../UnprocessableEntityException.php | 10 ++++++++ .../UnsupportedMediaTypeException.php | 10 ++++++++ 21 files changed, 223 insertions(+) create mode 100644 src/Network/HTTPException.php create mode 100644 src/Network/HTTPException/BadGatewayException.php create mode 100644 src/Network/HTTPException/BadRequestException.php create mode 100644 src/Network/HTTPException/ConflictException.php create mode 100644 src/Network/HTTPException/ExpectationFailedException.php create mode 100644 src/Network/HTTPException/ForbiddenException.php create mode 100644 src/Network/HTTPException/GatewayTimeoutException.php create mode 100644 src/Network/HTTPException/GoneException.php create mode 100644 src/Network/HTTPException/ImATeapotException.php create mode 100644 src/Network/HTTPException/InternalServerErrorException.php create mode 100644 src/Network/HTTPException/LenghtRequiredException.php create mode 100644 src/Network/HTTPException/MethodNotAllowedException.php create mode 100644 src/Network/HTTPException/NonAcceptableException.php create mode 100644 src/Network/HTTPException/NotFoundException.php create mode 100644 src/Network/HTTPException/NotImplementedException.php create mode 100644 src/Network/HTTPException/PreconditionFailedException.php create mode 100644 src/Network/HTTPException/ServiceUnavaiableException.php create mode 100644 src/Network/HTTPException/TooManyRequestsException.php create mode 100644 src/Network/HTTPException/UnauthorizedException.php create mode 100644 src/Network/HTTPException/UnprocessableEntityException.php create mode 100644 src/Network/HTTPException/UnsupportedMediaTypeException.php diff --git a/src/Network/HTTPException.php b/src/Network/HTTPException.php new file mode 100644 index 0000000000..eec4a9ced2 --- /dev/null +++ b/src/Network/HTTPException.php @@ -0,0 +1,23 @@ +httpdesc == "") { + $this->httpdesc = preg_replace("|([a-z])([A-Z])|",'$1 $2', str_replace("Exception","",get_class($this))); + } + parent::__construct($message, $code, $previous); + } +} diff --git a/src/Network/HTTPException/BadGatewayException.php b/src/Network/HTTPException/BadGatewayException.php new file mode 100644 index 0000000000..1bb8b29e98 --- /dev/null +++ b/src/Network/HTTPException/BadGatewayException.php @@ -0,0 +1,10 @@ + Date: Thu, 23 Nov 2017 23:47:52 -0500 Subject: [PATCH 2/4] Fix httpdesc to work with new namespace --- src/Network/HTTPException.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Network/HTTPException.php b/src/Network/HTTPException.php index eec4a9ced2..7602290c2b 100644 --- a/src/Network/HTTPException.php +++ b/src/Network/HTTPException.php @@ -11,12 +11,16 @@ namespace Friendica\Network; use Exception; -class HTTPException extends Exception { +class HTTPException extends Exception +{ var $httpcode = 200; var $httpdesc = ""; - public function __construct($message = "", $code = 0, Exception $previous = null) { - if ($this->httpdesc == "") { - $this->httpdesc = preg_replace("|([a-z])([A-Z])|",'$1 $2', str_replace("Exception","",get_class($this))); + + public function __construct($message = '', $code = 0, Exception $previous = null) + { + if ($this->httpdesc == '') { + $classname = str_replace('Exception', '', str_replace('Friendica\Network\HTTPException\\', '', get_class($this))); + $this->httpdesc = preg_replace("|([a-z])([A-Z])|",'$1 $2', $classname); } parent::__construct($message, $code, $previous); } From 1f4fc87fd995fd048fd8ea28d42709a7b0d2c4cb Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 23 Nov 2017 23:48:15 -0500 Subject: [PATCH 3/4] Use new HTTPExceptions in API --- include/api.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/include/api.php b/include/api.php index 50aa18c7e0..a5e8063848 100644 --- a/include/api.php +++ b/include/api.php @@ -12,11 +12,19 @@ use Friendica\Core\Config; use Friendica\Core\NotificationsManager; use Friendica\Core\Worker; use Friendica\Database\DBM; +use Friendica\Network\HTTPException; +use Friendica\Network\HTTPException\BadRequestException; +use Friendica\Network\HTTPException\ForbiddenException; +use Friendica\Network\HTTPException\InternalServerErrorException; +use Friendica\Network\HTTPException\MethodNotAllowedException; +use Friendica\Network\HTTPException\NotFoundException; +use Friendica\Network\HTTPException\NotImplementedException; +use Friendica\Network\HTTPException\UnauthorizedException; +use Friendica\Network\HTTPException\TooManyRequestsException; use Friendica\Object\Contact; use Friendica\Protocol\Diaspora; use Friendica\Util\XML; -require_once 'include/HTTPExceptions.php'; require_once 'include/bbcode.php'; require_once 'include/datetime.php'; require_once 'include/conversation.php'; From 9943393b71d844141fa39f739fa93042cf0b114e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 23 Nov 2017 23:52:23 -0500 Subject: [PATCH 4/4] Remove include/HTTPExceptions.php --- include/HTTPExceptions.php | 105 ------------------------------------- 1 file changed, 105 deletions(-) delete mode 100644 include/HTTPExceptions.php diff --git a/include/HTTPExceptions.php b/include/HTTPExceptions.php deleted file mode 100644 index 8571c99de5..0000000000 --- a/include/HTTPExceptions.php +++ /dev/null @@ -1,105 +0,0 @@ -httpdesc=="") { - $this->httpdesc = preg_replace("|([a-z])([A-Z])|",'$1 $2', str_replace("Exception","",get_class($this))); - } - parent::__construct($message, $code, $previous); - } -} - -// 4xx -class TooManyRequestsException extends HTTPException { - var $httpcode = 429; -} - -class UnauthorizedException extends HTTPException { - var $httpcode = 401; -} - -class ForbiddenException extends HTTPException { - var $httpcode = 403; -} - -class NotFoundException extends HTTPException { - var $httpcode = 404; -} - -class GoneException extends HTTPException { - var $httpcode = 410; -} - -class MethodNotAllowedException extends HTTPException { - var $httpcode = 405; -} - -class NonAcceptableException extends HTTPException { - var $httpcode = 406; -} - -class LenghtRequiredException extends HTTPException { - var $httpcode = 411; -} - -class PreconditionFailedException extends HTTPException { - var $httpcode = 412; -} - -class UnsupportedMediaTypeException extends HTTPException { - var $httpcode = 415; -} - -class ExpetationFailesException extends HTTPException { - var $httpcode = 417; -} - -class ConflictException extends HTTPException { - var $httpcode = 409; -} - -class UnprocessableEntityException extends HTTPException { - var $httpcode = 422; -} - -class ImATeapotException extends HTTPException { - var $httpcode = 418; - var $httpdesc = "I'm A Teapot"; -} - -class BadRequestException extends HTTPException { - var $httpcode = 400; -} - -// 5xx - -class ServiceUnavaiableException extends HTTPException { - var $httpcode = 503; -} - -class BadGatewayException extends HTTPException { - var $httpcode = 502; -} - -class GatewayTimeoutException extends HTTPException { - var $httpcode = 504; -} - -class NotImplementedException extends HTTPException { - var $httpcode = 501; -} - -class InternalServerErrorException extends HTTPException { - var $httpcode = 500; -} - - -