-
Notifications
You must be signed in to change notification settings - Fork 2.5k
add try/catch around statements that execute intl_is_failure() #6598
Conversation
You should not just catch the entire exception. I'll try and add some additional notes later. You should be able to detect the version and wrap it properly. You may want to create a new method for the call as to properly wrap it for existing behavior. |
@gkralik @mwillbanks there is the But it's only available PHP 5.5 + |
Other idea (should return no warning and proper handling later extending exceptions...) if(class_exists('IntlException') && $ex instanceof IntlException)){
//
} |
What's the benefit? We're throwing |
https://github.com/ThaDafinser/zf2/blob/patch-13/library/Zend/I18n/Validator/Float.php#L130 won't work for me. |
@gkralik updated it...was just freely coded |
@ThaDafinser OK, i like that. This way no exceptions get lost if they're not |
@gkralik please wait for additional feedback. To be honest i dont know if this is enough, since the |
Also the constanct |
you're right, seems to have changed quite a lot... |
OK, just checked that: Handling of Int and DateTime validators are still the same and need exception handling... |
I think @moderndeveloperllc can help, since he rewrote it |
Someone rang? Yes, I rewrote the beast called Float because it didn't actually validate everything it should have. I should get around to Int some day when I'm feeling particularly masochistic. I probably just forgot to remove the unused constant as they were both basically the same thing. Adding try/catch seems fine with me as long as it doesn't break older versions. It doesn't mess with the actual validation logic. Then again I'm on a rather powerful (prescribed thank you very much) painkiller at the moment, so pretty much anything sounds just fine right now :-) |
I have rebased the PR and added the check for non- |
@moderndeveloperllc Actually, shouldn't we set the |
throw new Exception\InvalidArgumentException($formatter->getErrorMessage()); | ||
} | ||
} catch(\Exception $e) { | ||
if(get_class($e) != 'IntlException'){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use instanceof
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. IntlException
is not available prior to PHP 5.5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is actually no problem, instanceof
also works when the class doesn't exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DASPRiD really!
http://3v4l.org/6VNuH
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, didn't know that. Will change it.
@gkralik This PR need to be rebased. |
Since PHP 5.5.0, the configuration option 'intl.use_exceptions' is available. When set to true, IntlException's are thrown instead of just setting a error code, so no calls to intl_is_failure() are necessary. Without the try/catch, the Int, Float and DateTime I18n validators hard-fail in case 'intl.use_exceptions' is set to true. NOTE: catching Exception (super class of IntlException), as IntlException is not available prior to PHP 5.5.0. refs #6312
@malukenho done. |
instanceof even works for PHP < 5.5 where IntlException does not exist, so use it instead of get_class().
if (intl_is_failure($formatter->getErrorCode())) { | ||
throw new ValidatorException\InvalidArgumentException($formatter->getErrorMessage()); | ||
} | ||
} catch(\Exception $e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gkralik well, I think you can't use pokémon exception handler
here.
What you think about use one more catch statement
?
<?php
try {
// ...
} catch (IntlException $e) {
} catch (Exception $e) {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good hint: only catch a specific exception type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking over this again, I agree. Best will be to just catch IntlException
here and ignore Exception
completely. It seems PHP versions < 5.5 will not choke on this.
Will need to write tests for this one before merging. |
…nces as previous exceptions
…ptions' into develop Close zendframework/zendframework#6598 Close zendframework/zendframework#6312 Forward port zendframework/zendframework#6598 Forward port zendframework/zendframework#6312
Since PHP 5.5.0, the configuration option 'intl.use_exceptions' is available.
When set to true, IntlException's are thrown instead of just setting a error
code, so no calls to intl_is_failure() are necessary.
Without the try/catch, the Int, Float and DateTime I18n validators hard-fail
in case 'intl.use_exceptions' is set to true.
NOTE: catching Exception (super class of IntlException), as IntlException is
not available prior to PHP 5.5.0.
refs #6312