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

Handle properties that throw KeyError #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dmfallak
Copy link
Contributor

@dmfallak dmfallak commented Sep 20, 2024

Problem:
Sometimes we can get exceptions like the following:

Traceback (most recent call last):
       <Redacted>
       ...
       File "/code/virtualenv_run/lib/python3.10/site-packages/pyramid_zipkin/tween.py", line 218, in tween
         get_binary_annotations(request, response),
       File "/code/virtualenv_run/lib/python3.10/site-packages/pyramid_zipkin/request_helper.py", line 173, in get_binary_annotations
         'network.protocol.version': request.http_version,
       File "/code/virtualenv_run/lib/python3.10/site-packages/webob/descriptors.py", line 44, in fget
         return req.environ[key]
     KeyError: 'SERVER_PROTOCOL'

Solution
Add a safety check around the property accesses.

Verification
Added a unit test to cover this case. Existing tests still pass.

'url.scheme': request.scheme,
'http.uri': request.path,
'http.uri.qs': request.path_qs,
'http.request.method': safe_get_property(request, 'method', 'unknown'),
Copy link
Contributor

Choose a reason for hiding this comment

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

getattr(obj, prop, default) provides a way to specify a default value if the attribute isn't found, hence avoiding the need for exception handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It still throws KeyError if we use getattr(obj, prop, default). I think it's because it's not that the property doesn't exist, but because this dictionary lookup fails: https://github.com/Pylons/webob/blob/5a67a56d32e3b7dd23b644b2c89736b799e50dc7/src/webob/descriptors.py#L24

@dmfallak dmfallak changed the title Handle nonexistent properties Handle properties that throw KeyError Sep 20, 2024
@EOjeah
Copy link
Member

EOjeah commented Sep 23, 2024

@dmfallak was working on a slightly different approach with #133

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