With the notable exception of rules_scheduler/rules_scheduler.rules.inc:176, the message specified as first parameter on thrown RulesException is not translated.
Do you think it's correct to add t() around exception messages?
The t() docs deprecate it's use on variables, so I think it shouldn't be put around getMessage() when the exception is printed.
Summary of final API changes:
- During evaluation RulesEvaluationException have to be thrown instead of RulesException.
- The validate callback of actions/conditions has to throw
RulesIntegrityExceptioninstead ofRulesExceptionnow.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | rules_exceptions.patch | 28.59 KB | fago |
| #15 | 1217504-rules-exceptions.patch | 27.75 KB | klausi |
| #14 | 1217504-rules-exceptions.patch | 27.42 KB | klausi |
| #8 | rules-error-translation.patch | 30.45 KB | aturetta |
| #6 | rules5-exception-translation.patch | 36.59 KB | aturetta |
Comments
Comment #1
fagohm, the RulesException is designed to not work that way. However, we want nicely translated and formatted validation errors in the UI, so we should make sure those get translated. The way the RulesException works is bad for that as potx would miss its strings, so in practice they probably won't get translated.
So let's better translate the text before it goes into the exception. I don't see any cause why we shouldn't translate the text before it goes into the exception? Validation-exceptions won't go into the watchdog usually anyway.
So we could
* add a new type of exception, RulesValidationException - which is intended for output and receives translated messages + optionally a form element / parameter for which to show the error?
As this is slight API change, we should do that before the release.
Comment #2
aturetta commentedI studied the code, and I think the attached patch is appropriate.
Now I've seen where t() is used in the current code, and of course no exception message can be known to the translation interface until it's thrown.
I haven't found any occurencies of Exceptions that wouldn't benefit from translation, so I modified RulesException instead. The code is much cleaner. I don't know if dependent modules might be affected, I only checked Commerce and it doesn't.
Fixed some typo and double quote in the process.
I think rules_log would benefit from the same treatment (will post a separate issue)
Angelo.
Comment #3
aturetta commentedForgot one more typo
Comment #4
aturetta commentedargggg
I swear I checked it before uploading, but still I missed some chunks
Comment #5
aturetta commenteddefinitely embarassing ... :-(
Comment #6
aturetta commentedNew, final, patch, $e->message is protected, use $e->getMessage() instead
I have ready a similar patch for rules_log() (based on this one), should I put the patch here or you prefer a new issue?
Comment #7
klausiThis should go to the faces project: http://drupal.org/project/faces
make sure to use spaces for indentation, not tabs.
missing space after the comma, see the Drupal coding standards: http://drupal.org/coding-standards
don't use single quotes here, as this may not be handled properly by .pot file generators for text translation. See http://drupal.org/coding-standards#quotes
Remarks apply to multiple locations in your patch.
Comment #8
aturetta commentedThanks @klausi for review
About point 1 (faces) the code is inside rules module, so I think it should be patched too.
Re-rolled correcting other notes from #7
Summary:
Comment #9
fagoI'm not sure about that.
This is also involves rules_log(), as exceptions go into the rules log. So if exceptions land translated there, regular log entries would have to do so too. However, that would negatively impact performance as it runs quite some t() calls on strings that are probably never displayed.
Grepping for exceptions in core, core isn't consistent with translating exceptions either. So I don't think there is cause to change this existing and working API now, when we should work towards a stable release.
Let's introduce something like a RulesValidationException for validate() that expects a translated exception message and optionally allows bind the exception to a certain parameter. That way we can use it to create form-errors marking the form of the parameter as cause.
@faces:
yes, but we should fix at /project/faces first and then take over the changes in rules.
Comment #10
fagoComment #11
aturetta commentedI think ALL the current uses of RulesException involves validation, so I don't understand what benefit would come from the residual RulesException.
Re: logs, I have a patch ready for this too, but I understand that t() would then be called multiple times even if debug log is disabled.
(BTW, why are tests not running here)
Comment #12
fago>I think ALL the current uses of RulesException involves validation, so I don't understand what benefit would come from the residual RulesException.
No, there are other use-cases. RulesExceptions are thrown if something cannot be executed. But you are right, during integrityCheck() the exceptions thrown are basically the same as for validate(). So what about using a translated RulesIntegrityException for both cases while staying with RulesException for everything that involves execution? We could even go and rename it to RulesEvaluationException as I don't think it's used outside for Rules now. (API changes.. :(°°°)
>(BTW, why are tests not running here)
The test bot stopped working for projects with dependencies :/
Comment #13
fagoWe need to obey the general plans for error handling here, as I've outlined at #1142852-9: improve erros and Rules evaluation log handling. We certainly need the ability to log errors to watchdog, for which we need the non-translated exception as we have it now.
Comment #14
klausiPatch that splits up RulesException in 4 different exceptions:
* RulesException, abstract mother class (catch all)
* RulesEvaluationException, thrown during Rules execution. Does not use t() in the constructor for logging.
* RulesIntegrityExecption, thrown during configuration and validation, translateable end user UI messages
* RulesDependencyException, thrown if a module is missing during configuration
Not yet heavily tested.
Comment #15
klausijust a slight documentation improvement.
Comment #16
fagoThe first line of comments should not contain any new-lines, but may exceed 80chars. Fixed that.
Also, I think the dependency exception should extend the integrity-exception, as it is just a special case of it.
Comment #17
fagore-rolled patch fixing the issues, including some doc improvements.
Comment #18
klausiIf the tests are green this is RTBC
Comment #19
fagothanks committed + added api changes summary to the issue summary.
Comment #20.0
(not verified) commentedadded api changes