Skip to content

Commit

Permalink
fix: handle edge case where no response body is recieved
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jun 15, 2024
1 parent 015d072 commit 418ba2e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Api/Services/BaseGeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function get(string $ip): ?ServiceResponse
$this->updateRateLimitsFromResponse($response);
}

$body = json_decode($response->getBody());
$body = json_decode($response->getBody()->getContents());

if ($this->hasError($response, $body)) {
$this->logger->error("Error detected in response from {$this->host}");
Expand Down Expand Up @@ -140,7 +140,7 @@ abstract protected function buildBatchUrl(array $ips, ?string $apiKey): string;

abstract protected function getRequestOptions(?string $apiKey, array $ips = null): array;

abstract protected function hasError(ResponseInterface $response, object $body): bool;
abstract protected function hasError(ResponseInterface $response, mixed $body): bool;

abstract protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse;

Expand Down
4 changes: 2 additions & 2 deletions src/Api/Services/IPApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ protected function getRequestOptions(?string $apiKey, array $ips = null): array
];
}

protected function hasError(ResponseInterface $response, object $body): bool
protected function hasError(ResponseInterface $response, mixed $body): bool
{
$allowedFailures = ['reserved range', 'private range'];

return $body->status === 'fail' && !in_array($body->message, $allowedFailures);
return $body?->status === 'fail' && !in_array($body->message, $allowedFailures);
}

protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Services/IPData.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ protected function getRequestOptions(?string $apiKey, array $ips = null): array
];
}

protected function hasError(ResponseInterface $response, object $body): bool
protected function hasError(ResponseInterface $response, mixed $body): bool
{
return isset($body->error) || ($response->getStatusCode() >= 400 && !Str::contains($body->message, ['is a reserved IP address']));
return isset($body?->error) || ($response->getStatusCode() >= 400 && !Str::contains($body?->message, ['is a reserved IP address']));
}

protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Services/IPLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function isRateLimited(): bool
return false;
}

protected function hasError(ResponseInterface $response, object $body): bool
protected function hasError(ResponseInterface $response, mixed $body): bool
{
return $body->response_code !== '200';
return $body?->response_code !== '200';
}

protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Services/IPSevenEx.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function getRequestOptions(?string $apiKey, array $ips = null): array
];
}

protected function hasError(ResponseInterface $response, object $body): bool
protected function hasError(ResponseInterface $response, mixed $body): bool
{
return $response->getStatusCode() > 200;
}
Expand Down

0 comments on commit 418ba2e

Please sign in to comment.