Skip to content

Commit

Permalink
Update exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jul 10, 2023
1 parent 01223ae commit 2ccd0a9
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 18 deletions.
25 changes: 16 additions & 9 deletions src/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,48 @@

namespace Jenky\Atlas\Exception;

use Jenky\Atlas\Response;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;

class HttpException extends RuntimeException implements ClientExceptionInterface
{
/**
* The response instance.
*
* @var \Jenky\Atlas\Response
* @var ResponseInterface
*/
private $response;

public function __construct(Response $response)
{
parent::__construct($this->prepareMessage($response), $response->status());
public function __construct(
ResponseInterface $response,
string $message = '',
?\Throwable $previous = null
) {
parent::__construct(
$message ?: $this->prepareMessage($response),
$response->getStatusCode(),
$previous
);

$this->response = $response;
}

/**
* Get the response.
*/
public function response(): Response
public function getResponse(): ResponseInterface
{
return $this->response;
}

/**
* Prepare the exception message.
*/
protected function prepareMessage(Response $response): string
protected function prepareMessage(ResponseInterface $response): string
{
return sprintf('HTTP request returned status code %d %s: %s',
$response->status(), $response->reason(), $response->body()
return sprintf('HTTP request returned status code %d %s',
$response->getStatusCode(), $response->getReasonPhrase()
);
}
}
4 changes: 3 additions & 1 deletion src/Exception/NotDecodableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Jenky\Atlas\Exception;

class NotDecodableException extends \LogicException
use Psr\Http\Client\ClientExceptionInterface;

class NotDecodableException extends \LogicException implements ClientExceptionInterface
{
public static function create(string $message = 'Unable to decode the response body.'): self
{
Expand Down
44 changes: 44 additions & 0 deletions src/Exception/RequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Jenky\Atlas\Exception;

use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class RequestException extends \RuntimeException implements RequestExceptionInterface
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var null|ResponseInterface
*/
private $response;

public function __construct(
string $message,
RequestInterface $request,
?ResponseInterface $response = null,
?\Throwable $previous = null,
) {
$code = $response ? $response->getStatusCode() : 0;
parent::__construct($message, $code, $previous);
$this->request = $request;
$this->response = $response;
}

public function getRequest(): RequestInterface
{
return $this->request;
}

public function getResponse(): ?ResponseInterface
{
return $this->response;
}
}
2 changes: 1 addition & 1 deletion src/Exception/RequestRetryFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Jenky\Atlas\Exception;

final class RequestRetryFailedException extends \RuntimeException
class RequestRetryFailedException extends RequestException
{
}
2 changes: 1 addition & 1 deletion src/Exception/TooManyRedirectsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Jenky\Atlas\Exception;

class TooManyRedirectsException extends \Exception
class TooManyRedirectsException extends RequestException
{
}
6 changes: 3 additions & 3 deletions src/Middleware/FollowRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ public function __invoke(RequestInterface $request, callable $next): ResponseInt
return $response;
}

$this->guardMax();
$this->guardMax($request, $response);

return $this($this->modifyRequest($request, $response), $next);
}

private function guardMax(): void
private function guardMax(RequestInterface $request, ResponseInterface $response): void
{
++$this->redirects;

if ($this->redirects > $this->max) {
throw new TooManyRedirectsException('');
throw new TooManyRedirectsException("Will not follow more than {$this->max} redirects", $request, $response);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Middleware/RetryRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function __invoke(RequestInterface $request, callable $next): ResponseInt

if ($stop) {
if ($this->context->throwable()) {
throw new RequestRetryFailedException(sprintf('Maximum %d retries reached.', $this->context->maxRetries()));
throw new RequestRetryFailedException(
sprintf('Maximum %d retries reached.', $this->context->maxRetries()),
$request,
$response,
);
}

return $response;
Expand Down
4 changes: 2 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ public function getResponse(): ResponseInterface
public function toException(): ?HttpException
{
if ($this->clientError()) {
return new Exception\ClientRequestException($this);
return new Exception\ClientRequestException($this->response);
}

if ($this->serverError()) {
return new Exception\ServerRequestException($this);
return new Exception\ServerRequestException($this->response);
}

return null;
Expand Down

0 comments on commit 2ccd0a9

Please sign in to comment.