I want to detect errors in a standalone Python script with Sentry+Raven.
I tried to configure it and raven test ...
is workging.
Then I place this on top of the script:
from raven import Client
client = Client('http://...@.../1')
client.captureException()
the exception is generated later on this:
import django
django.setup()
from django.conf import settings
And I want to see the actual stack for this error:
ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named 'settings'
But all I see in Sentry is
which is completely useless.
How can I change this to have a normal traceback?
You misunderstand how client.captureException()
works, its not a configuration parameter. You use it when you are catching an exception and it will capture the exception type and message:
try:
f = open('oogah-boogah.txt')
except IOError:
client.captureException()
# do something here
To capture any exceptions that could be generated in a block of code, you can use capture_exceptions
:
@client.capture_exceptions
def load_django():
import django
django.setup()
from django.conf import settings
Yes you're right, but is there a way to catch an exception not wrapping a block of code in a try-except. I can see the error in a terminal, can I see it in Sentry?
There is a default exception handler - and when an exception is not caught, this default handler catches it and then displays the exception. This is what you see in the terminal.
The function that generates this output is sys.excepthook
and it will output to stderr
by default.
So, in order for you to catch all exception globally, you'll have to create a global exception handler or map your own function to sys.excepthook
.
I would strongly recommend against this, though as you don't know what other side effects it may have.