Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.2] add support for optional custom error message for Configura... #5094

Closed
wants to merge 5 commits into from

Conversation

cordoval
Copy link
Contributor

RFC: ref #1666

Basically this is the start, but i wanted to ask which one of these options is better:

  1. extend/override the base Exception class to include the custom error message
    this requires a modification of the throw specific expection passing Exception($this) meaning it is getting
    the current node or the node from which the exception is being thrown
  2. propagate the changes in every class that throws exception similar than this PR
    this would require changes in every particular class

Please vote whether 1 or 2 is more fit

Thanks to all symfony2 community :D

@cordoval
Copy link
Contributor Author

cordoval commented Oct 2, 2012

@stof any updates on this one? or is left to starve to death?

@stof
Copy link
Member

stof commented Oct 2, 2012

@cordoval I don't think adding yet another method is a good idea. A better solution would be to use the info attached to the node to enhance the error message

@fabpot
Copy link
Member

fabpot commented Oct 3, 2012

+1 for @stof idea.

@fabpot
Copy link
Member

fabpot commented Oct 5, 2012

@cordoval Can you work on an evolution of this PR based on the feedback?

@cordoval
Copy link
Contributor Author

cordoval commented Oct 5, 2012

@stof @fabpot yes i will start tonight sorry for the delay 👍

@fabpot
Copy link
Member

fabpot commented Oct 5, 2012

@cordoval Thanks a lot.

@cordoval
Copy link
Contributor Author

cordoval commented Oct 7, 2012

I have thought in two ways to refactor:

$message = sprintf(
    'Invalid configuration for path "%s": %s',
    $this->getPath(),
    $invalid->getMessage()
);
if ($this->getInfo() != null) {
    $message .= sprintf("\nError message: %s.", $this->getInfo());
}
$ex = new InvalidConfigurationException($message, $invalid->getCode(), $invalid);
$ex->setPath(...)

throw $ex;

into

$message = sprintf(
    'Invalid configuration for path "%s": %s',
    $this->getPath(),
    $invalid->getMessage()
)

$ex = new InvalidConfigurationException($this->addErrorHintMessageIfPresent($message), $invalid->getCode(), $invalid);
$ex->setPath(...)

throw $ex;

and have the helper method to the BaseNode class to avoid locs everywhere

or even implementing this into the exception base class or some other but then do:

$ex->setPath(...);
$ex->setInfo($this->getInfo());

Any preference? @stof @fabpot

@cordoval
Copy link
Contributor Author

cordoval commented Oct 7, 2012

I will also work on changes to the documentation after this is re-factored and put into shape for merge.

@schmittjoh
Copy link
Contributor

I don't think this works that well. With such an implementation, all you could add is a description of the node in question.

The reason why error message does not work is simply that there might be many reasons for why something is going wrong. So, for each of these reasons, you would need to add a different message. If you really would like to do that, this could work by adding first an id for each message that could occur (or generate one), and then having a map on the node which replaces the generic message with the custom one. This would be a bit of work though.

If you instead only would like to add a description of the node, I'm not really convinced this is useful either as the error message still is the generic one, and not a custom error message.

@cordoval
Copy link
Contributor Author

cordoval commented Oct 7, 2012

@schmittjoh the idea for the info in this case is to provide a sample peek of what the configuration looks like or a hint why usually things went wrong in terms of how the configuration is used. We are not trying to map a message to every problem as in the case where we have constraints and we want to specify things. I think for that we have the custom nodes that can be extended for a given specific data constraint/requirements, like the NumericNode or IntegerNode which were PRed. This is a small tiny helper hint to have. But yeah i agree with you that it is no more than that.

Maybe @lsmith77 wanted to have just that or maybe more? I think this is like the run-time version of config:dump-reference command.

@lsmith77
Copy link
Contributor

lsmith77 commented Oct 7, 2012

@schmittjoh if there are multiple causes you can list them all. i expect this to mostly be used to clarify things based on issue tickets. ie. users express some confusion about an error message, and then you add some clarifications. sort of an evolutionary process.

@@ -205,6 +205,9 @@ protected function finalizeValue($value)
if (!array_key_exists($name, $value)) {
if ($child->isRequired()) {
$msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath());
if ($this->getInfo() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be null !== $this->getInfo()

@stof
Copy link
Member

stof commented Oct 13, 2012

@cordoval Please update all nodes according to my comments and then squash your commits.

@fabpot
Copy link
Member

fabpot commented Dec 6, 2012

What is the status of this PR?

@cordoval
Copy link
Contributor Author

cordoval commented Dec 6, 2012

@fabpot i will review it tonight thanks

@cordoval
Copy link
Contributor Author

cordoval commented Dec 7, 2012

gah, i got swamped at my work, anyone be willing to take a look and what is left please? if not then i can say i can try looking at beginning of next week

@cordoval
Copy link
Contributor Author

cordoval commented Dec 8, 2012

@fabpot ok iam working on this now, i got time, i will basically finish the tests which i think is the only thing left

@cordoval
Copy link
Contributor Author

cordoval commented Dec 9, 2012

👍 /cc @stof @lsmith77

@fabpot
Copy link
Member

fabpot commented Dec 10, 2012

Looks good to me but we repeat the same code over and over again. Can we try to make the code more future proof. what about adding a setHint() method to the exception that does the work?

…info

[symfony#1666] RFC: add support for optional custom error message for Configuration validation errors
[symfony#1666] remove method that was previously added on NodeDefinition since we will reuse info method
[symfony#1666] remove errorMessage methods introduced in BaseNode class since we are going through info this time
[symfony#1666] reuse getInfo message for error output in exception for ScalarNode class
[symfony#1666] clean up of ScalarNode adding of info exception message
[symfony#1666] reuse getInfo message for error output in exception for VariableNode class
[symfony#1666] correct a typo explicited -> made explicit
[symfony#1666] reuse getInfo message for error output in exception for NumericNode class
[symfony#1666] reuse getInfo message for error output in exception for FloatNode and IntegerNode
[symfony#1666] reuse getInfo message for error output in exception for EnumNode class
[symfony#1666] reuse getInfo message for error output in exception for BooleanNode class
[symfony#1666] reuse getInfo message for error output in exception for BaseNode class
[symfony#1666] reuse getInfo message for error output in exception for ArrayNode class
[symfony#1666] remove test on TreeBuilder test getInfo since it becomes redundant as info already is tested
[symfony#1666] fix test to use getInfo for ScalarNodeTest
address comments
swtich to if (->getInfo() != null) {
@cordoval
Copy link
Contributor Author

@fabpot working on this right now

@cordoval
Copy link
Contributor Author

@fabpot ok done, the error is because of another failing test in master, but so everything else is passing.

👍 thanks for helping me out, I think some more refactoring could be done but it could also be overkill

So all good now

@@ -204,9 +204,12 @@ protected function finalizeValue($value)
foreach ($this->children as $name => $child) {
if (!array_key_exists($name, $value)) {
if ($child->isRequired()) {
$msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath());
$ex = new InvalidConfigurationException($msg);
$ex = new InvalidConfigurationException(sprintf('The child node "%s" at path "%s" must be configured.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert to everything on one line? (same below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do the same for the other exceptions? Thanks.

@fabpot
Copy link
Member

fabpot commented Jan 9, 2013

@cordoval Any news on my comments?

@cordoval
Copy link
Contributor Author

cordoval commented Jan 9, 2013

@fabpot can this still get on 2.2 release?

@fabpot
Copy link
Member

fabpot commented Jan 9, 2013

Yes, but it must be mergeable before Friday.

@fabpot
Copy link
Member

fabpot commented Apr 22, 2013

@cordoval Any chance you address my latest comments? I'm about to close it as this is not something we must have.

@fabpot
Copy link
Member

fabpot commented Aug 9, 2013

Closing as this code have serious drawbacks. Feel free to reopen whenever the code is ready.

@fabpot fabpot closed this Aug 9, 2013
@cordoval
Copy link
Contributor Author

cordoval commented Aug 9, 2013

ok @fabpot i will bookmark this to work on, sorry about the delay, 👶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants