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

Feature: custom exception for session configuration errors #3627

Open
jvanasco opened this issue Nov 3, 2020 · 0 comments
Open

Feature: custom exception for session configuration errors #3627

jvanasco opened this issue Nov 3, 2020 · 0 comments

Comments

@jvanasco
Copy link
Contributor

jvanasco commented Nov 3, 2020

Feature Request

When there is a configuration issue, such as no session factory configured, Pyramid raises a standard AttributeError. See:

@reify
def session(self):
"""Obtain the :term:`session` object associated with this
request. If a :term:`session factory` has not been registered
during application configuration, a
:class:`pyramid.exceptions.ConfigurationError` will be raised"""
factory = self.registry.queryUtility(ISessionFactory)
if factory is None:
raise AttributeError(
'No session factory registered '
'(see the Sessions chapter of the Pyramid documentation)'
)
return factory(self)

It would be beneficial if Pyramid were to raise a custom subclass of AttributeError. It could be something as generic as this:

class PyramidConfigurationError(AttributeError):
    pass

This would be backwards compatible with existing code, but allow better control over detecting a non-configured session.

Right now, the exception string must be analyzed

try:
    session = request.session
    ...
    do things
    ...
except AttributeError as exc:
	if exc.args[0].startswith("No session factory registered"):
		do_something()
	else:
		do_something_else()

It would be preferable to catch it directly, like:

try:
    session = request.session
except PyramidConfigurationError:
	do_something()

I'm not sure how many other Pyramid components would benefit from this, so I understand the concern over its overall utility.

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

No branches or pull requests

1 participant