Pylons / pyramid

Pyramid - A Python web framework
https://trypyramid.com/
Other
3.97k stars 887 forks source link

Feature: custom exception for session configuration errors #3627

Open jvanasco opened 4 years ago

jvanasco commented 4 years ago

Feature Request

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

https://github.com/Pylons/pyramid/blob/68a6cb1dba7944f1e214ca62ce700e3f7ddf618d/src/pyramid/request.py#L189-L201

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.