4848import java .util .stream .Collectors ;
4949
5050public class GeoIPFilter implements Closeable {
51+
52+ // This exception could raise during the processing of datapoint with custom fields, check out
53+ // for more details https://github.com/logstash-plugins/logstash-filter-geoip/issues/226
54+ static class GeoIp2InvalidCustomFieldException extends GeoIp2Exception {
55+ public GeoIp2InvalidCustomFieldException (Throwable cause ) {
56+ super ("invalid custom field" , cause );
57+ }
58+ }
59+
5160 private static final Logger logger = LogManager .getLogger ();
5261 private final String sourceField ;
5362 private final String targetField ;
@@ -152,7 +161,7 @@ public boolean handleEvent(RubyEvent rubyEvent) {
152161 throw new IllegalArgumentException ("Expected input field value to be String or List type" );
153162 }
154163
155- if (ip .trim ().isEmpty ()){
164+ if (ip .trim ().isEmpty ()) {
156165 return false ;
157166 }
158167
@@ -224,7 +233,12 @@ private boolean applyGeoData(Map<Field, Object> geoData, Event event) {
224233 }
225234
226235 private Map <Field ,Object > retrieveCityGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
227- CityResponse response = databaseReader .city (ipAddress );
236+ CityResponse response ;
237+ try {
238+ response = databaseReader .city (ipAddress );
239+ } catch (NullPointerException e ) {
240+ throw new GeoIp2InvalidCustomFieldException (e );
241+ }
228242 Country country = response .getCountry ();
229243 City city = response .getCity ();
230244 Location location = response .getLocation ();
@@ -337,7 +351,12 @@ private Map<Field,Object> retrieveCityGeoData(InetAddress ipAddress) throws GeoI
337351 }
338352
339353 private Map <Field ,Object > retrieveCountryGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
340- CountryResponse response = databaseReader .country (ipAddress );
354+ CountryResponse response ;
355+ try {
356+ response = databaseReader .country (ipAddress );
357+ } catch (NullPointerException e ) {
358+ throw new GeoIp2InvalidCustomFieldException (e );
359+ }
341360 Country country = response .getCountry ();
342361 Continent continent = response .getContinent ();
343362 Map <Field , Object > geoData = new EnumMap <>(Field .class );
@@ -372,7 +391,12 @@ private Map<Field,Object> retrieveCountryGeoData(InetAddress ipAddress) throws G
372391 }
373392
374393 private Map <Field , Object > retrieveIspGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
375- IspResponse response = databaseReader .isp (ipAddress );
394+ IspResponse response ;
395+ try {
396+ response = databaseReader .isp (ipAddress );
397+ } catch (NullPointerException e ) {
398+ throw new GeoIp2InvalidCustomFieldException (e );
399+ }
376400
377401 Map <Field , Object > geoData = new EnumMap <>(Field .class );
378402 for (Field desiredField : this .desiredFields ) {
@@ -411,7 +435,12 @@ private Map<Field, Object> retrieveIspGeoData(InetAddress ipAddress) throws GeoI
411435 }
412436
413437 private Map <Field , Object > retrieveAsnGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
414- AsnResponse response = databaseReader .asn (ipAddress );
438+ AsnResponse response ;
439+ try {
440+ response = databaseReader .asn (ipAddress );
441+ } catch (NullPointerException e ) {
442+ throw new GeoIp2InvalidCustomFieldException (e );
443+ }
415444 Network network = response .getNetwork ();
416445
417446 Map <Field , Object > geoData = new EnumMap <>(Field .class );
@@ -444,7 +473,12 @@ private Map<Field, Object> retrieveAsnGeoData(InetAddress ipAddress) throws GeoI
444473 }
445474
446475 private Map <Field , Object > retrieveDomainGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
447- DomainResponse response = databaseReader .domain (ipAddress );
476+ DomainResponse response ;
477+ try {
478+ response = databaseReader .domain (ipAddress );
479+ } catch (NullPointerException e ) {
480+ throw new GeoIp2InvalidCustomFieldException (e );
481+ }
448482 Map <Field , Object > geoData = new EnumMap <>(Field .class );
449483 for (Field desiredField : this .desiredFields ) {
450484 switch (desiredField ) {
@@ -459,7 +493,12 @@ private Map<Field, Object> retrieveDomainGeoData(InetAddress ipAddress) throws G
459493 }
460494
461495 private Map <Field , Object > retrieveEnterpriseGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
462- EnterpriseResponse response = databaseReader .enterprise (ipAddress );
496+ EnterpriseResponse response ;
497+ try {
498+ response = databaseReader .enterprise (ipAddress );
499+ } catch (NullPointerException e ) {
500+ throw new GeoIp2InvalidCustomFieldException (e );
501+ }
463502
464503 Map <Field , Object > geoData = new EnumMap <>(Field .class );
465504 Country country = response .getCountry ();
@@ -567,7 +606,12 @@ private Map<Field, Object> retrieveEnterpriseGeoData(InetAddress ipAddress) thro
567606 }
568607
569608 private Map <Field , Object > retrieveAnonymousIpGeoData (final InetAddress ipAddress ) throws GeoIp2Exception , IOException {
570- AnonymousIpResponse response = databaseReader .anonymousIp (ipAddress );
609+ AnonymousIpResponse response ;
610+ try {
611+ response = databaseReader .anonymousIp (ipAddress );
612+ } catch (NullPointerException e ) {
613+ throw new GeoIp2InvalidCustomFieldException (e );
614+ }
571615
572616 Map <Field , Object > geoData = new EnumMap <>(Field .class );
573617 boolean isHostingProvider = response .isHostingProvider ();
0 commit comments