Opened 4 months ago

Last modified 2 weeks ago

#36775 assigned Cleanup/optimization

Raise ImproperlyConfigured when Feed.link is missing instead of failing with AttributeError

Reported by: yureiblack Owned by: yureiblack
Component: contrib.syndication Version: dev
Severity: Normal Keywords:
Cc: yureiblack, jaffar Khan Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacob Walls)

Currently, the Feed.get_feed() method retrieves the feed's link using:

    link = self._get_dynamic_attr("link", obj)

If a Feed subclass does not define a link attribute, link becomes None.
This results in an unhelpful exception when Django later calls:

    link = add_domain(current_site.domain, link, request.is_secure())

Since add_domain() calls url.startswith(), a missing link
causes:

    AttributeError: 'NoneType' object has no attribute 'startswith'

This error does not clearly indicate the actual problem.

According to Django documentation, every Feed class must define
link. Therefore, it should raise a more appropriate and explicit
exception.

The attached PR adds the following check:

    if link is None:
        raise ImproperlyConfigured(
            "Feed class must define a 'link' attribute."
        )

This results in a clearer error message and avoids unexpected crashes.

PR: https://github.com/django/django/pull/20371

Change History (4)

comment:1 by Jacob Walls, 4 months ago

Description: modified (diff)
Needs tests: set
Owner: set to yureiblack
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Seems reasonable, thanks. (I think this is the "better error" described as "wouldn't kill us" in a very similar scenario in ticket:2218#comment:1.)

Could you include a test?

comment:2 by jaffar Khan, 2 months ago

Can I work on this ticket since the current owner no longer seems to be active?

comment:3 by jaffar Khan, 2 months ago

Cc: jaffar Khan added

comment:4 by Vinay Datta, 2 weeks ago

Hi, I’ve worked on this issue and implemented a fix.

Previously, when Feed.link was missing, it resulted in an AttributeError. I’ve added an explicit check to ensure that link is present (and handled whether it is callable or not). Now, if it is missing or evaluates to an empty value, it raises ImproperlyConfigured with a clear error message instead.

I’ve also verified that the change prevents the unintended AttributeError and provides a more appropriate and developer-friendly exception.

Please let me know if you’d like me to add or adjust tests for this behavior before I open a PR.

Note: See TracTickets for help on using tickets.
Back to Top