fix: guard malformed postcodes.io responses and isolate persist errors from HTTP success
This commit is contained in:
@@ -122,6 +122,10 @@ class PostcodeService
|
|||||||
|
|
||||||
$data = $response->json('result');
|
$data = $response->json('result');
|
||||||
|
|
||||||
|
if (! is_array($data) || ! isset($data['postcode'], $data['latitude'], $data['longitude'])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$result = new LocationResult(
|
$result = new LocationResult(
|
||||||
query: $postcode,
|
query: $postcode,
|
||||||
displayName: $data['postcode'],
|
displayName: $data['postcode'],
|
||||||
@@ -129,6 +133,7 @@ class PostcodeService
|
|||||||
lng: $data['longitude'],
|
lng: $data['longitude'],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
Postcode::updateOrCreate(
|
Postcode::updateOrCreate(
|
||||||
['postcode' => $normalised],
|
['postcode' => $normalised],
|
||||||
[
|
[
|
||||||
@@ -137,6 +142,12 @@ class PostcodeService
|
|||||||
'lng' => $data['longitude'],
|
'lng' => $data['longitude'],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
Log::warning('PostcodeService: failed to persist postcode after HTTP fallback', [
|
||||||
|
'postcode' => $normalised,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
@@ -163,6 +174,10 @@ class PostcodeService
|
|||||||
|
|
||||||
$data = $response->json('result');
|
$data = $response->json('result');
|
||||||
|
|
||||||
|
if (! is_array($data) || ! isset($data['outcode'], $data['latitude'], $data['longitude'])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$result = new LocationResult(
|
$result = new LocationResult(
|
||||||
query: $outcode,
|
query: $outcode,
|
||||||
displayName: $data['outcode'],
|
displayName: $data['outcode'],
|
||||||
@@ -170,6 +185,7 @@ class PostcodeService
|
|||||||
lng: $data['longitude'],
|
lng: $data['longitude'],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
Outcode::updateOrCreate(
|
Outcode::updateOrCreate(
|
||||||
['outcode' => $normalised],
|
['outcode' => $normalised],
|
||||||
[
|
[
|
||||||
@@ -177,6 +193,12 @@ class PostcodeService
|
|||||||
'lng' => $data['longitude'],
|
'lng' => $data['longitude'],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
Log::warning('PostcodeService: failed to persist outcode after HTTP fallback', [
|
||||||
|
'outcode' => $normalised,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user