1

A funny thing I've noticed about Django's flatpages app: it allows core/handlers/base.py to log a warning Not Found: $page. As a result of that, my Sentry logs are full with 404s for legitimate and working pages. It seems to happen because first Django logs a 404, then it returns a HttpResponseNotFound object and then the flatpages middleware kicks in and returns a proper 200 response.

Is this something I could consider a bug in Django? My reasoning is that a valid flatpage is not a missing page and thus shouldn't log a 404 message. Isn't there an other way to catch a 404 without logging it as missing?

Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97

2 Answers2

2

I think the solution to your problem is easy: just re-order the Sentry404CatchMiddleware to the top of your MIDDLEWARES setting. At the very least, it should be above the flatpages middleware.

To explain what's going on, it's helpful to understand the order in which middlewares are executed. I'm guessing you followed the Sentry docs and placed it at the bottom. That makes it the first middleware to be executed. If a request comes in for an unmatched URL pattern, Django raises a 404 and the Sentry middleware logs it. But Django then runs through the other middlewares, and the flatpages middleware does it's thing where it looks up if a matching page exists, and actually replaces the response.

If you move the Sentry middleware to the top, only 404 errors that bubble all the way through the middleware stack will get logged, which is likely what you want.

Maik Hoepfel
  • 1,747
  • 1
  • 13
  • 19
1

It's not a bug, it's the way that django flatpages app work: its middleware kicks in after 404 from urls. That's why your sentry is full of 404s.

Consider not registering 404 in sentry. :/ I dont see any other way here.

There might be another solution: instead of using middleware try to include flatpages.urls at end of your urlpatterns.

seler
  • 8,803
  • 4
  • 41
  • 54
  • The problem is: I want to register my 404s. I just don't want to the flatpages in there. – Nikolai Prokoschenko Oct 11 '11 at 14:19
  • @rassie, there might be another solution: instead of using middleware try to include flatpages.urls at the end of your urlpatterns. – seler Oct 12 '11 at 20:26
  • For the reference, including flatpages.urls will break APPEND_SLASH for anything that's not a flatpage. In cases where you need APPEND_SLASH, flatpages and all 404s in your logs, you need to roll your own replacement of CommonMiddleware in your custom view for flatpages or create a middleware for flatpages with process_request. – koniiiik Nov 02 '12 at 18:18