-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Check if port is int and raise exception #8575
Conversation
Thanks for submitting a pull request 🚀 @ArjaanBuijk will take a look at it as soon as possible ✨ |
@ancalita , |
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.
Thanks for your contribution! 🚀
In addition to adding a unit test to reproduce the Sentry error which checks that the added code catches it and raises RasaException, please also add a changelog entry at rasa/changelog
by following the README instructions at that location.
rasa/core/brokers/pika.py
Outdated
@@ -169,6 +170,10 @@ async def _connect(self) -> aio_pika.RobustConnection: | |||
last_exception = None | |||
for _ in range(self._connection_attempts): | |||
try: | |||
|
|||
if not isinstance(self.port, int): | |||
raise RasaException("Port has to be a integer.") |
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.
I think the RasaException can be raised a bit earlier - for example in the constructor L75 where you can wrap the integer casting in a try-except block:
try:
self.port = int(port)
except ValueError as e:
raise RasaException("Port could not be converted to integer.") from e
You can try this by adding a new unit test at rasa/tests/core/test_broker.py
and reproducing the Sentry error behaviour to see if this try-except block catches it.
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, thanks for the tips, would it be something like this?
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 was fast ⚡
It's looking in the right direction - I would also add an extra assertion after pytest.raises:
with pytest.raises(RasaException) as e:
# to do
assert "Port could not be converted to integer." in str(e.value)
…ES/rasa into 8309/check_if_port_is_int
tests/core/test_broker.py
Outdated
@@ -310,6 +311,23 @@ async def test_no_pika_logs_if_no_debug_mode(caplog: LogCaptureFixture): | |||
for record in caplog.records | |||
) | |||
|
|||
broker = PikaEventBroker( | |||
"host", "username", "password", retry_delay_in_seconds=1, connection_attempts=1 | |||
) |
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.
I'm not exactly sure what these new lines are for 🤔 If you added by mistake, could you please remove?
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.
@jppgomes This also needs to be resolved.
tests/core/test_broker.py
Outdated
) | ||
|
||
|
||
def test_raise_exception_port(monkeypatch: MonkeyPatch): |
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.
If there is no need for MonkeyPatch, this argument can be removed.
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.
yes.. sorry, fixed!!
tests/core/test_broker.py
Outdated
def test_raise_exception_port(monkeypatch: MonkeyPatch): | ||
|
||
with pytest.raises(RasaException): | ||
PikaEventBroker( |
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.
I'm aware you cannot see the full stacktrace from Sentry, not sure why it got clipped, but I would actually use exactly what gets called further up in the stacktrace: the EventBroker factory from here.
You can create an instance of EndpointConfig which you can pass as argument to the broker factory:
EndpointConfig(username="username", password="password", type="pika", port="PORT")
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.
In addition, note of caution - the EventBroker factory is async so you'll need to use the keyword await
when calling it. As well as making the test async too.
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, i'll do this
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.
@ancalita can you check if it is right now?
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.
@jppgomes Sure, you could also re-request review by clicking on the arrows-in-a-circle icon to the right of the picture avatar under Reviewers section (top right).
Does the test pass?
Please also add this assertion check in the test:
with pytest.raises(RasaException) as e:
...
assert "Port could not be converted to integer." in str(e.value)
@jppgomes There appears to be an issue with the license agreement not signed - did you commit changes from different devices maybe? You need to make sure that you've set an email address on your machine - this is an useful guide. |
…ES/rasa into 8309/check_if_port_is_int
tests/core/test_broker.py
Outdated
@@ -22,6 +22,7 @@ | |||
from rasa.core.brokers.kafka import KafkaEventBroker | |||
from rasa.core.brokers.pika import PikaEventBroker, DEFAULT_QUEUE_NAME | |||
from rasa.core.brokers.sql import SQLEventBroker | |||
from rasa.shared.exceptions import RasaException |
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.
i just noticed there is actually an existing import from the same module in L27 - could you add the , RasaException
to L27 and remove duplicate import in L25?
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
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.
Looks good, thank you 💯
Hey @jppgomes I see you've run into some flakey CI test failures, it's fine to restart the jobs: go to Details of any failed check and you'll see somewhere on the right side Re-run jobs, select Re-run all jobs and that will restart the process. If you have this issue again, let me know and I can have a look. |
Hi @jppgomes could you please update branch with the base branch, that's something i can't do - I'll monitor the CI tests and restart if necessary. |
ok @ancalita |
@ancalita it looks like everything is fine now |
Proposed changes:
Status (please check what you already did):
black
(please check Readme for instructions)