Skip to content

Commit

Permalink
Add FailedRemoteRequest interface to mark exception classes related t…
Browse files Browse the repository at this point in the history
…o remote requests
  • Loading branch information
pierlon committed Apr 2, 2020
1 parent e87ab88 commit af0b450
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/common/phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ parameters:
- %currentWorkingDirectory%/src/
autoload_files:
- %currentWorkingDirectory%/vendor/autoload.php
ignoreErrors:
- '#^PHPDoc tag @throws with type AmpProject\\Exception\\FailedRemoteRequest is not subtype of Throwable$#'
13 changes: 13 additions & 0 deletions lib/common/src/Exception/FailedRemoteRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace AmpProject\Exception;

/**
* Marker interface to enable consumers to catch all exceptions for failed remote requests.
*
* @package ampproject/common
*/
interface FailedRemoteRequest extends AmpException
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package ampproject/common
*/
final class FailedToGetCachedResponseData extends RuntimeException implements AmpException
final class FailedToGetCachedResponse extends RuntimeException implements FailedRemoteRequest
{

/**
Expand All @@ -20,7 +20,7 @@ final class FailedToGetCachedResponseData extends RuntimeException implements Am
*/
public static function withUrl($url)
{
$message = "Failed to retrieve the cached response data for the URL '{$url}'.";
$message = "Failed to retrieve the cached response for the URL '{$url}'.";

return new self($message);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/src/Exception/FailedToGetFromRemoteUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @package ampproject/common
*/
final class FailedToGetFromRemoteUrl extends RuntimeException implements AmpException
final class FailedToGetFromRemoteUrl extends RuntimeException implements FailedRemoteRequest
{

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/common/src/RemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmpProject;

use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Exception\FailedRemoteRequest;

/**
* Interface for abstracting away the transport that is being used for making remote requests.
Expand All @@ -19,7 +19,7 @@ interface RemoteGetRequest
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url);
}
3 changes: 2 additions & 1 deletion lib/common/src/RemoteRequest/CurlRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function __construct($sslVerify = true, $timeout = self::DEFAULT_TIMEOUT,
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/common/src/RemoteRequest/FallbackRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
use Exception;
Expand Down Expand Up @@ -55,7 +55,7 @@ private function addRemoteGetRequestInstance(RemoteGetRequest $remoteGetRequest)
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
3 changes: 2 additions & 1 deletion lib/common/src/RemoteRequest/FilesystemRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct($argumentMap)
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/common/src/RemoteRequest/StubbedRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
use LogicException;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function __construct($argumentMap)
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
7 changes: 4 additions & 3 deletions src/RemoteRequest/CachedRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace AmpProject\AmpWP\RemoteRequest;

use AmpProject\Exception\FailedToGetCachedResponseData;
use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\Exception\FailedToGetCachedResponse;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\RemoteRequest\RemoteGetRequestResponse;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function __construct(
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetCachedResponseData If retrieving the contents from the cache failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get( $url ) {
$cache_key = self::TRANSIENT_PREFIX . md5( __CLASS__ . $url );
Expand Down Expand Up @@ -140,7 +141,7 @@ public function get( $url ) {
}

if ( ! $cached_response->is_valid() ) {
throw new FailedToGetCachedResponseData( $url );
throw new FailedToGetCachedResponse( $url );
}

return new RemoteGetRequestResponse( $cached_response->get_body(), $cached_response->get_headers(), $cached_response->get_status_code() );
Expand Down
3 changes: 2 additions & 1 deletion src/RemoteRequest/WpHttpRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace AmpProject\AmpWP\RemoteRequest;

use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\RemoteRequest\RemoteGetRequestResponse;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function __construct( $ssl_verify = true, $timeout = self::DEFAULT_TIMEOU
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get( $url ) {
$retries_left = $this->retries;
Expand Down

0 comments on commit af0b450

Please sign in to comment.