Skip to content

Exception not requests.Response with BaseNomadException str dunder#109

Merged
erhlee-bird merged 3 commits intomasterfrom
107-bug-fix
Apr 1, 2020
Merged

Exception not requests.Response with BaseNomadException str dunder#109
erhlee-bird merged 3 commits intomasterfrom
107-bug-fix

Conversation

@jonathanrcross
Copy link
Copy Markdown
Collaborator

@jonathanrcross jonathanrcross commented Mar 31, 2020

Addressing #107, add safety checks to verify that the passed parameter of nomad_resp is a requests.Response object if it is not cast str around exception with different message.

Not a huge fan of have a conditional str with different messages (along with checking for certain substrings as a test) but this should at least fix the bug and ensure somewhat backwards compatibility. Technically text in requests.Response is not a guarantee either since its kwargs are popped for response and request and can possibly be None.

If we do eventually move to python 3+ only support adding annotations should help a bunch when visually inspecting or even mypy level runtime checking.

Copy link
Copy Markdown
Collaborator

@erhlee-bird erhlee-bird left a comment

Choose a reason for hiding this comment

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

I have the same reactions to you about the Exception substring tests and conditional __str__, but it looks very reasonable to me.

Comment thread tests/test_base.py

try:
n = nomad.Nomad(
host="162.16.10.102",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, is this an actual host of importance or just a dummy value?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Completely arbitrary dummy value!

Comment thread tests/test_base.py Outdated

_ = n.job.deregister_job("example")

except nomad.api.exceptions.BaseNomadException as err:
Copy link
Copy Markdown
Collaborator

@jeffwecan jeffwecan Mar 31, 2020

Choose a reason for hiding this comment

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

assertRaises as a context manager ends up being a clearer way of handling this type of test case imho.

Edit: beyond that, it's more effective and guarding against unintended changes in behavior. That is, this test case ensures we get the behavior we expect when an exception is raised. However if an exception isn't raised at all (for whatever reason) it will still pass, no?

Copy link
Copy Markdown
Collaborator Author

@jonathanrcross jonathanrcross Mar 31, 2020

Choose a reason for hiding this comment

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

Completely agree with your points, I don't know why I decided not to use pytest.raises context manager, since ive used it elsewhere throughout the tests corpus.. must have been the "lateness of the night" :| , I'll swap that out!

Copy link
Copy Markdown
Collaborator

@erhlee-bird erhlee-bird left a comment

Choose a reason for hiding this comment

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

I like the change made as suggested by @jeffwecan with the context manager for the exception.

Comment thread nomad/api/exceptions.py

def __str__(self):
return 'The {0} was raised with following response: {1}.'.format(self.__class__.__name__, self.nomad_resp.text)
if isinstance(self.nomad_resp, requests.Response) and hasattr(self.nomad_resp, "text"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It does seem rather funky to have this logic be part of the exception itself. I am tempted to suggest it would be better to tackle things from the other direction. For example something like:

However I also feel like that sort of change introduces its own fiddly bits for various reasons so I can live with this route :)

@erhlee-bird erhlee-bird merged commit fe3b6ab into master Apr 1, 2020
@jrxFive jrxFive deleted the 107-bug-fix branch September 1, 2021 20:36
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.

3 participants