Skip to content

Commit

Permalink
Merge pull request #29 from ipinfo/uman/exceptions
Browse files Browse the repository at this point in the history
Allow suppressing exceptions
  • Loading branch information
UmanShahzad authored Nov 19, 2021
2 parents 8a8da3d + d44acc9 commit 0d94570
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `ipinfolaravel` will be documented in this file.

## v2.2.0

- Added the `no_except` config variable which allows suppressing exceptions
that occur in the IPinfo middleware; the `$request->ipinfo` object will be
`null` in this case.

## v2.1.3

- The IPinfo PHP SDK will no longer be initialized multiple times - one will be
Expand Down
15 changes: 14 additions & 1 deletion src/ipinfolaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ public function handle($request, Closure $next)
if ($this->filter && call_user_func($this->filter, $request)) {
$details = null;
} else {
$details = $this->ipinfo->getDetails($request->ip());
try {
$details = $this->ipinfo->getDetails($request->ip());
} catch (\Exception $e) {
$details = null;

// users can't catch this exception with their own wrapper
// middleware unfortunately, so we catch it for them. but for
// backwards-compatibility, we throw the exception again unless
// they've told us not to.
if ($this->no_except != true) {
throw $e;
}
}
}

$request->request->set('ipinfo', $details);
Expand All @@ -57,6 +69,7 @@ public function configure()
{
$this->access_token = config('services.ipinfo.access_token', null);
$this->filter = config('services.ipinfo.filter', [$this, 'defaultFilter']);
$this->no_except = config('services.ipinfo.no_except', false);

if ($custom_countries = config('services.ipinfo.countries_file', null)) {
$this->settings['countries_file'] = $custom_countries;
Expand Down
4 changes: 4 additions & 0 deletions testapp/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'ipinfo' => [
'no_except' => true,
'access_token' => env('IPINFO_TOKEN'),
],
];

0 comments on commit 0d94570

Please sign in to comment.