From 412c4eb579b0e92c6cc184bdc6248957bb9facd2 Mon Sep 17 00:00:00 2001 From: Bradley Date: Thu, 26 Sep 2024 10:35:22 +0100 Subject: [PATCH] Tweak how errors are thrown --- src/Service/Geocoder.php | 13 ++++++++++--- tests/Service/GeocoderTest.php | 8 ++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Service/Geocoder.php b/src/Service/Geocoder.php index 90d0d21..899a86e 100644 --- a/src/Service/Geocoder.php +++ b/src/Service/Geocoder.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pcm\GeocodeBundle\Service; use Pcm\GeocodeBundle\Exception\ApiErrorException; +use Pcm\GeocodeBundle\Exception\MissingGeocodeDataException; use Pcm\GeocodeBundle\Exception\NoResultsFoundException; use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface; use Pcm\GeocodeBundle\Model\GeoCoordinates; @@ -32,17 +33,23 @@ final class Geocoder */ public function geocode(GeocodeInterface $entity): GeoCoordinates { - $geocodeData = $entity->getGeocodeData(); + $geocodeData = trim($entity->getGeocodeData()); $client = $this->createClient(); + + if ('' === $geocodeData) { + throw new MissingGeocodeDataException('No geocode data present.'); + } + $response = $this->makeApiRequest($client, $geocodeData); $data = $this->getDataFromResponse($response); if (array_key_exists('error', $data)) { - throw new ApiErrorException($geocodeData, $data['error']['message']); + throw new ApiErrorException($data['error']['message']); } if (empty($data)) { - throw new NoResultsFoundException($geocodeData, "No results found."); + $message = sprintf('No results found with geocode data "%s".', $geocodeData); + throw new NoResultsFoundException($message); } return $this->createGeoCoordinates($data); diff --git a/tests/Service/GeocoderTest.php b/tests/Service/GeocoderTest.php index f8729e7..df564a0 100644 --- a/tests/Service/GeocoderTest.php +++ b/tests/Service/GeocoderTest.php @@ -4,9 +4,9 @@ declare(strict_types=1); namespace Pcm\GeocodeBundle\Tests; +use Pcm\GeocodeBundle\Exception\MissingGeocodeDataException; use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface; use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait; -use Pcm\GeocodeBundle\Exception\ApiErrorException; use Pcm\GeocodeBundle\Exception\NoResultsFoundException; use Pcm\GeocodeBundle\Model\GeoCoordinates; use Pcm\GeocodeBundle\Service\Geocoder; @@ -33,9 +33,9 @@ final class GeocoderTest extends KernelTestCase public function testGeocodeThrowsOnEmptyInput(): void { sleep(1); - $this->expectException(ApiErrorException::class); + $this->expectException(MissingGeocodeDataException::class); $entity = $this->createEntity(''); - $this->expectExceptionMessageMatches("/Nothing to search for.$/"); + $this->expectExceptionMessageMatches("/No geocode data present./"); $this->geocoder->geocode($entity); } @@ -43,7 +43,7 @@ final class GeocoderTest extends KernelTestCase { sleep(1); $this->expectException(NoResultsFoundException::class); - $this->expectExceptionMessageMatches("/No results found.$/"); + $this->expectExceptionMessageMatches("/No results found with geocode data \"Invalid Postcode\"./"); $entity = $this->createEntity('Invalid Postcode'); $this->geocoder->geocode($entity); }