Skip to content

Fix message for catching wrong exception in raises() #1252

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

Merged
merged 2 commits into from
Sep 1, 2021

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Aug 18, 2021

The original implementation of raises() in c6a793d apparently referenced an undeclared $exceptionname variable several times. That was later addressed 804f383 but that commit neglected to fix this reference.

This change avoids a possible message where the actual exception is printed twice:

ALMOST: Got Exception - expected Exception

Given the error handler implementation here, we should always except an ErrorException.

The original implementation of raises() in c6a793d apparently referenced an undeclared $exceptionname variable several times. That was later addressed 804f383 but that commit neglected to fix this reference.
@jmikola jmikola requested a review from tanlisu August 18, 2021 18:10
@@ -607,7 +607,7 @@ function raises($function, $type, $infunction = null) {
}
printf("OK: Got %s\n", severityToString($e->getSeverity()));
} else {
printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname);
printf("ALMOST: Got %s - expected %s\n", get_class($e), ErrorException::class);
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this is not a complete fix. This else condition is also reached if the severity (e.g. notice, warning) is incorrect. I'll revise that in a bit.

Copy link
Member

Choose a reason for hiding this comment

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

Yes. In this case, I think we can change the catch in L593 to only catch ErrorException, so we only have to handle the case where we've caught messages with a different severity.

We should also wrap the restore_error_handler call in a finally block for the try to properly restore the error handler regardless of outcome.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added finally with restore_error_handler. I also improved output for converting severity to a string and refactored both raises() and throws() to reduce conditional nesting.

@jmikola jmikola removed the request for review from tanlisu August 18, 2021 18:11
@jmikola jmikola changed the title Fix message for catching wrong exception in raises() [DRAFT] Fix message for catching wrong exception in raises() Aug 18, 2021
@jmikola jmikola requested a review from alcaeus September 1, 2021 02:14
@jmikola jmikola changed the title [DRAFT] Fix message for catching wrong exception in raises() Fix message for catching wrong exception in raises() Sep 1, 2021
@jmikola jmikola force-pushed the raises-error-message branch from 04ee9bf to 7cbf854 Compare September 1, 2021 14:05
@jmikola jmikola force-pushed the raises-error-message branch from 7cbf854 to 9e15119 Compare September 1, 2021 17:22
@jmikola jmikola merged commit f663761 into master Sep 1, 2021
@jmikola jmikola deleted the raises-error-message branch September 1, 2021 22:32
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.

2 participants