Skip to content

Commit 06d5bf8

Browse files
committed
Adds error handling
1 parent ac5e0c3 commit 06d5bf8

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/Services/IPAddressDriver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Zaengle\LaravelSecurityNotifications\Services;
44

5+
use Exception;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Facades\Http;
78
use Zaengle\LaravelSecurityNotifications\Jobs\ProcessNewIPAddress;
@@ -20,7 +21,9 @@ public function __construct(
2021

2122
public function handle(): void
2223
{
23-
$ipLocationData = Http::get('http://ip-api.com/json/'.$this->ipAddress)->json();
24+
$ipLocationData = Http::retry(3)->get('http://ip-api.com/json/'.$this->ipAddress)?->json();
25+
26+
throw_if(is_null($ipLocationData), new Exception('Failed to get IP location data for: '.$this->ipAddress));
2427

2528
$loginQuery = Login::query()
2629
->where([

tests/Unit/IPAddressHandlerTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Http\Client\PendingRequest;
34
use Illuminate\Support\Facades\Bus;
45
use Illuminate\Support\Facades\Config;
56
use Illuminate\Support\Facades\Http;
@@ -20,6 +21,8 @@
2021

2122
$user = User::factory()->create();
2223

24+
Http::shouldReceive('retry')
25+
->andReturnSelf();
2326
Http::shouldReceive('get')
2427
->with('http://ip-api.com/json/127.0.0.1')
2528
->once()
@@ -57,6 +60,8 @@
5760
'last_login_at' => now()->subDays(5),
5861
]);
5962

63+
Http::shouldReceive('retry')
64+
->andReturnSelf();
6065
Http::shouldReceive('get')
6166
->with('http://ip-api.com/json/'.$login->ip_address)
6267
->once()
@@ -94,6 +99,8 @@
9499
],
95100
]);
96101

102+
Http::shouldReceive('retry')
103+
->andReturnSelf();
97104
Http::shouldReceive('get')
98105
->with('http://ip-api.com/json/127.0.0.1')
99106
->once()
@@ -131,6 +138,8 @@
131138
],
132139
]);
133140

141+
Http::shouldReceive('retry')
142+
->andReturnSelf();
134143
Http::shouldReceive('get')
135144
->with('http://ip-api.com/json/127.0.0.1')
136145
->once()
@@ -187,6 +196,8 @@
187196
],
188197
]);
189198

199+
Http::shouldReceive('retry')
200+
->andReturnSelf();
190201
Http::shouldReceive('get')
191202
->with('http://ip-api.com/json/128.0.0.1')
192203
->once()
@@ -231,4 +242,21 @@
231242
IPAddress::process([
232243
'ipAddress' => '127.0.0.1',
233244
]);
234-
})->expectException(IPAddressDriverMissingException::class);
245+
})->expectException(IPAddressDriverMissingException::class);
246+
247+
it('fails if no ip location data', function () {
248+
$login = Login::factory()->create();
249+
250+
Http::shouldReceive('retry')
251+
->andReturnSelf();
252+
Http::shouldReceive('get')
253+
->with('http://ip-api.com/json/1234567890');
254+
Http::shouldReceive('json')
255+
->never();
256+
257+
IPAddress::process([
258+
'ipAddress' => '1234567890',
259+
'userId' => $login->user_id,
260+
'userType' => $login->user_type,
261+
]);
262+
})->expectException(Exception::class, 'Failed to get IP location data for: 1234567890');

0 commit comments

Comments
 (0)