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,14 +133,21 @@ class PostcodeService
|
|||||||
lng: $data['longitude'],
|
lng: $data['longitude'],
|
||||||
);
|
);
|
||||||
|
|
||||||
Postcode::updateOrCreate(
|
try {
|
||||||
['postcode' => $normalised],
|
Postcode::updateOrCreate(
|
||||||
[
|
['postcode' => $normalised],
|
||||||
'outcode' => substr($normalised, 0, strlen($normalised) - 3),
|
[
|
||||||
'lat' => $data['latitude'],
|
'outcode' => substr($normalised, 0, strlen($normalised) - 3),
|
||||||
'lng' => $data['longitude'],
|
'lat' => $data['latitude'],
|
||||||
],
|
'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,13 +185,20 @@ class PostcodeService
|
|||||||
lng: $data['longitude'],
|
lng: $data['longitude'],
|
||||||
);
|
);
|
||||||
|
|
||||||
Outcode::updateOrCreate(
|
try {
|
||||||
['outcode' => $normalised],
|
Outcode::updateOrCreate(
|
||||||
[
|
['outcode' => $normalised],
|
||||||
'lat' => $data['latitude'],
|
[
|
||||||
'lng' => $data['longitude'],
|
'lat' => $data['latitude'],
|
||||||
],
|
'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