Throw exceptions when something goes wrong instead of returning null
This commit is contained in:
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Service;
|
||||
|
||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
@@ -26,13 +28,12 @@ final class Geocoder
|
||||
$response = $this->makeApiRequest($client, $postcode);
|
||||
$data = $this->getDataFromResponse($response);
|
||||
|
||||
// @todo remove check, I don't think it's needed
|
||||
if (empty($data)) {
|
||||
return null;
|
||||
if (array_key_exists('error', $data)) {
|
||||
throw new ApiErrorException($postcode, $data['error']['message']);
|
||||
}
|
||||
|
||||
if (array_key_exists('error', $data)) {
|
||||
return null;
|
||||
if (empty($data)) {
|
||||
throw new NoResultsFoundException($postcode, "No results found.");
|
||||
}
|
||||
|
||||
return $this->createGeoCoordinates($data);
|
||||
|
||||
Reference in New Issue
Block a user