Skip to content

Commit

Permalink
feat: better geoip2 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AutisticShark committed Mar 31, 2023
1 parent c556524 commit a63d0fa
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Utils/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,30 @@ final class Tools
public static function getIpLocation($ip): string
{
$geoip = new GeoIP2();
$err_msg = '';
$city = null;
$country = null;

if ($_ENV['maxmind_license_key'] === '') {
$err_msg = 'GeoIP2 服务未配置';
}

try {
$city = $geoip->getCity($ip);
$country = $geoip->getCountry($ip);
} catch (AddressNotFoundException|InvalidDatabaseException $e) {
return '未知';
$err_msg = '未知错误';
}

if ($city !== null) {
return $city . ', ' . $country;
}

return $country;
if ($country !== null) {
return $country;
}

return $err_msg;
}

/**
Expand Down

0 comments on commit a63d0fa

Please sign in to comment.