0

I want to catch each and every exception (including indentation error in views), where can I catch them all ?

I have written a middleware, it catches few exceptions and not all of them.

Note: IndentationError in the title is just an example, I want to catch each and every exception in the application.

vedarthk
  • 1,253
  • 1
  • 14
  • 34
  • 1
    Why do you even want to do this? A `SyntaxError` or `IndentationError` means you have basic problems with your code; and these should _never_ be "excepted" (that is caught and then processed manually). If you are trying to log errors in middleware, use an error service like [sentry](https://getsentry.com/welcome/). – Burhan Khalid Nov 22 '13 at 07:47
  • @BurhanKhalid `IndentationError` was just an example. Sentry is a third party service and I don't want to use that. I have built my own custom error aggregation tool which aggregates errors from Django application. And yes even I know `SyntaxError` is something bad in the code, it was just an example of an exception there can be other exceptions too such as `ConnectionTimedOut` for external services etc. – vedarthk May 28 '15 at 12:40

3 Answers3

2

IndentationError is a SyntaxError and it would be a horrible idea to catch Syntax Errors. Code must be validated before it can be executed.

Check this question Is IndentationError a syntax error in Python or not?

Also you can not caught SyntaxError unless that happens during an eval, compile etc. More information is here

Community
  • 1
  • 1
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
  • Yes I know that, IndentationError is a SyntaxError, it was just an example. The thing is I want to catch any kind of exception, it is possible if you connect to `django.core.signals.got_request_exception`. – vedarthk Nov 22 '13 at 07:51
1

You can install https://pypi.python.org/pypi/pep8.
It will make sure that that your project is following every pep8 standard .
It will also show you if any Indentation error.

Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
-1

After debugging I found that signals are sent to django.core.signals.got_request_exception for all exceptions that occur in Django i.e. we can catch all exceptions once we connect a function to that signal.

vedarthk
  • 1,253
  • 1
  • 14
  • 34