Skip to content

Commit

Permalink
Merge pull request #103 from cego/gusb/handle-guzzle-exception
Browse files Browse the repository at this point in the history
gusb/handle-guzzle-exception
  • Loading branch information
GustavBruhns authored Mar 21, 2024
2 parents ed5af33 + 20c79c9 commit d7b1623
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/RequestInsurance/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;

class HttpResponse
{
protected Response $response;
protected ConnectException $connectException;
protected RequestException $requestException;

/**
* @param Response|ConnectException $response
* @param Response|ConnectException|RequestException $response
*/
public function __construct($response)
{
if ($response instanceof ConnectException) {
$this->connectException = $response;
} elseif ($response instanceof RequestException) {
$this->requestException = $response;
} elseif ($response !== null) {
$this->response = $response;
}
Expand Down Expand Up @@ -59,6 +63,18 @@ public function isTimedOut(): bool
return isset($this->connectException);
}

/**
* Returns true when the request failed due to a Guzzle RequestException.
* The RequestException is a generalization of some other Guzzle exceptions,
* specifically BadResponseException, TooManyRedirectsException.
*
* @return bool
*/
public function isRequestException(): bool
{
return isset($this->requestException);
}

/**
* Logs the reason for the inconsistent state if the response is in an inconsistent state
*
Expand All @@ -72,9 +88,17 @@ public function logInconsistentReason(): void

if ($this->isTimedOut()) {
Log::error($this->connectException);
} else {
Log::error('No response object nor connect exception received for request');

return;
}

if ($this->isRequestException()) {
Log::error($this->requestException);

return;
}

Log::error('No response object, connect exception nor request exception was received for request');
}

/**
Expand Down Expand Up @@ -150,6 +174,10 @@ public function getBody(): ?string
return '<REQUEST_TIMED_OUT : THIS MESSAGE WAS ADDED BY REQUEST INSURANCE>';
}

if ($this->isRequestException()) {
return '<REQUEST_EXCEPTION : THIS MESSAGE WAS ADDED BY REQUEST INSURANCE>';
}

if ($this->isInconsistent()) {
return '<REQUEST_INCONSISTENT : THIS MESSAGE WAS ADDED BY REQUEST INSURANCE>';
}
Expand All @@ -174,7 +202,7 @@ public function getHeaders(): Collection
}

/**
* Gets the execution tim of the request
* Gets the execution time of the request
*
* @return float
*/
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/HttpResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Tests\TestCase;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Cego\RequestInsurance\HttpResponse;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\TooManyRedirectsException;

class HttpResponseTest extends TestCase
{
/** @test */
public function it_can_initialize_with_request_exception()
{
$httpResponse = new HttpResponse(new RequestException('F', new Request('GET', 'www.notaplaceforsho.dk')));

$this->assertTrue($httpResponse->isRequestException());
}

/** @test */
public function it_can_initialize_with_bad_response_exception()
{
$httpResponse = new HttpResponse(new BadResponseException('F', new Request('GET', 'www.notaplaceforsho.dk'), new Response()));

$this->assertTrue($httpResponse->isRequestException());
}

/** @test */
public function it_can_initialize_with_too_many_redirects_exception()
{
$httpResponse = new HttpResponse(new TooManyRedirectsException('F', new Request('GET', 'www.notaplaceforsho.dk')));

$this->assertTrue($httpResponse->isRequestException());
}
}

0 comments on commit d7b1623

Please sign in to comment.