سؤال

Why this code:

geoip_country_code_by_name('unknown'); 

generate ErrorException, when must return false ?

هل كانت مفيدة؟

المحلول

This is a bug in GeoIP package and is not fixed in any release (<= 1.0.8). It's fixed in the trunk however (see this revision). You can solve this by compiling the source from the latest trunk.

Edited: thanks to Wrikken for pointing out how Laravel handles errors.

With GeoIP <= 1.0.8 geoip_country_code_by_name will trigger an error (E_NOTICE) whenever the name cannot be found. Laravel will always set error_reporting to -1 and handle all errors (even notices) and translate them into ErrorExceptions. Normally one can catch ErrorExceptions using a try-catch block, but in this case it is not possible because Laravel never throws the exception, it just translate it for displaying and logging purposes.

It is possible to ignore the error with the @-operator. It's a bit bad to do so since it will ignore all errors that the function might throw. In this case however, the only other error geoip_country_code_by_name can trigger is warning when the database can't be reached. Therefore you can safely ignore the error if you make sure the database is available: (Code not tested)

if (geoip_db_avail(GEOIP_COUNTRY_EDITION))
{
    @geoip_country_code_by_name('unknown');
}
else
{
    // Throw exception or handle the error
    throw new Exception(
       "Required database not available at " . 
       geoip_db_filename(GEOIP_COUNTRY_EDITION) 
    );

}

Edit: Laravel now throws the ErrorException so that one can catch it using a try-catch block. At the time of writing, this change is not yet in any released tag. But a catching errors will probably work with Laravel/Framework >= 4.0.8.

نصائح أخرى

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top