1
0
Fork 0

Improved http error handling

This commit is contained in:
Michael 2021-10-29 23:21:07 +00:00
commit 4236a9a105
55 changed files with 282 additions and 135 deletions

View file

@ -22,8 +22,7 @@
namespace Friendica\Network;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\UnprocessableEntityException;
use Friendica\Util\Network;
/**
@ -102,7 +101,7 @@ class CurlResult implements IHTTPResult
* @param string $url optional URL
*
* @return IHTTPResult a CURL with error response
* @throws InternalServerErrorException
* @throws UnprocessableEntityException
*/
public static function createErrorCurl($url = '')
{
@ -117,12 +116,12 @@ class CurlResult implements IHTTPResult
* @param int $errorNumber the error number or 0 (zero) if no error
* @param string $error the error message or '' (the empty string) if no
*
* @throws InternalServerErrorException when HTTP code of the CURL response is missing
* @throws UnprocessableEntityException when HTTP code of the CURL response is missing
*/
public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
{
if (!array_key_exists('http_code', $info)) {
throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
throw new UnprocessableEntityException('CURL response doesn\'t contains a response HTTP code');
}
$this->returnCode = $info['http_code'];

View file

@ -31,15 +31,11 @@ use Exception;
*/
abstract class HTTPException extends Exception
{
public $httpdesc = '';
public $httpdesc = '';
public $explanation = '';
public function __construct($message = '', Exception $previous = null)
{
parent::__construct($message, $this->code, $previous);
if (empty($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);
}
}
}

View file

@ -26,4 +26,5 @@ use Friendica\Network\HTTPException;
class AcceptedException extends HTTPException
{
protected $code = 202;
var $httpdesc = 'Accepted';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class BadGatewayException extends HTTPException
{
protected $code = 502;
protected $code = 502;
var $httpdesc = 'Bad Gateway';
var $explanation = 'The server was acting as a gateway or proxy and received an invalid response from the upstream server.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class BadRequestException extends HTTPException
{
protected $code = 400;
protected $code = 400;
var $httpdesc = 'Bad Request';
var $explanation = 'The server cannot or will not process the request due to an apparent client error.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class ConflictException extends HTTPException
{
protected $code = 409;
protected $code = 409;
var $httpdesc = 'Conflict ';
var $explanation = 'Indicates that the request could not be processed because of conflict in the current state of the resource, such as an edit conflict between multiple simultaneous updates.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class ExpectationFailedException extends HTTPException
{
protected $code = 417;
protected $code = 417;
var $httpdesc = 'Expectation Failed';
var $explanation = 'The server cannot meet the requirements of the Expect request-header field.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class ForbiddenException extends HTTPException
{
protected $code = 403;
protected $code = 403;
var $httpdesc = 'Forbidden';
var $explanation = 'The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account.';
}

View file

@ -0,0 +1,30 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPException;
use Friendica\Network\HTTPException;
class FoundException extends HTTPException
{
protected $code = 302;
var $httpdesc = 'Found (Moved Temporarily)';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class GatewayTimeoutException extends HTTPException
{
protected $code = 504;
protected $code = 504;
var $httpdesc = 'Gateway Timeout';
var $explanation = 'The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class GoneException extends HTTPException
{
protected $code = 410;
protected $code = 410;
var $httpdesc = 'Gone';
var $explanation = 'Indicates that the resource requested is no longer available and will not be available again.';
}

View file

@ -25,6 +25,7 @@ use Friendica\Network\HTTPException;
class ImATeapotException extends HTTPException
{
protected $code = 418;
var $httpdesc = "I'm A Teapot";
protected $code = 418;
var $httpdesc = "I'm A Teapot";
var $explanation = 'This is a teapot that is requested to brew coffee.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class InternalServerErrorException extends HTTPException
{
protected $code = 500;
protected $code = 500;
var $httpdesc = 'Internal Server Error';
var $explanation = 'An unexpected condition was encountered and no more specific message is suitable.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class LenghtRequiredException extends HTTPException
{
protected $code = 411;
protected $code = 411;
var $httpdesc = 'Length Required';
var $explanation = 'The request did not specify the length of its content, which is required by the requested resource.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class MethodNotAllowedException extends HTTPException
{
protected $code = 405;
protected $code = 405;
var $httpdesc = 'Method Not Allowed';
var $explanation = 'A request method is not supported for the requested resource; for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource.';
}

View file

@ -0,0 +1,30 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPException;
use Friendica\Network\HTTPException;
class MovedPermanentlyException extends HTTPException
{
protected $code = 301;
var $httpdesc = 'Moved Permanently';
}

View file

@ -26,4 +26,5 @@ use Friendica\Network\HTTPException;
class NoContentException extends HTTPException
{
protected $code = 204;
var $httpdesc = 'No Content';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class NonAcceptableException extends HTTPException
{
protected $code = 406;
protected $code = 406;
var $httpdesc = 'Not Acceptable';
var $explanation = 'The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class NotFoundException extends HTTPException
{
protected $code = 404;
protected $code = 404;
var $httpdesc = 'Not Found';
var $explanation = 'The requested resource could not be found but may be available in the future.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class NotImplementedException extends HTTPException
{
protected $code = 501;
protected $code = 501;
var $httpdesc = 'Not Implemented';
var $explanation = 'The server either does not recognize the request method, or it lacks the ability to fulfil the request. Usually this implies future availability (e.g., a new feature of a web-service API).';
}

View file

@ -0,0 +1,30 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPException;
use Friendica\Network\HTTPException;
class NotModifiedException extends HTTPException
{
protected $code = 304;
var $httpdesc = 'Not Modified';
}

View file

@ -26,4 +26,5 @@ use Friendica\Network\HTTPException;
class OKException extends HTTPException
{
protected $code = 200;
var $httpdesc = 'OK';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class PreconditionFailedException extends HTTPException
{
protected $code = 412;
protected $code = 412;
var $httpdesc = 'Precondition Failed';
var $explanation = 'The server does not meet one of the preconditions that the requester put on the request header fields.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class ServiceUnavailableException extends HTTPException
{
protected $code = 503;
protected $code = 503;
var $httpdesc = 'Service Unavailable';
var $explanation = 'The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.';
}

View file

@ -0,0 +1,30 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Network\HTTPException;
use Friendica\Network\HTTPException;
class TemporaryRedirectException extends HTTPException
{
protected $code = 307;
var $httpdesc = 'Temporary Redirect';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class TooManyRequestsException extends HTTPException
{
protected $code = 429;
protected $code = 429;
var $httpdesc = 'Too Many Requests';
var $explanation = 'The user has sent too many requests in a given amount of time.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class UnauthorizedException extends HTTPException
{
protected $code = 401;
protected $code = 401;
var $httpdesc = 'Unauthorized';
var $explanation = 'Authentication is required and has failed or has not yet been provided.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class UnprocessableEntityException extends HTTPException
{
protected $code = 422;
protected $code = 422;
var $httpdesc = 'Unprocessable Entity';
var $explanation = 'The request was well-formed but was unable to be followed due to semantic errors.';
}

View file

@ -25,5 +25,7 @@ use Friendica\Network\HTTPException;
class UnsupportedMediaTypeException extends HTTPException
{
protected $code = 415;
protected $code = 415;
var $httpdesc = 'Unsupported Media Type';
var $explanation = 'The request entity has a media type which the server or resource does not support. For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.';
}