How to get uWSGI Python exception message pretty printing? - python

Would like to know if there is a simple, easy way to have uWSGI pretty print exception messages (for Python specifically, not sure if the settings are particular to Python or not).
Thanks very much!

If you mean getting the exception message in the browser, just add --catch-exceptions
IMPORTANT: it could expose sensitive informations, do not use in production !!!

Related

Is there any python exception notifier present?

What is my requirement ?
--> I need Exception notifier which will email to some specific configured user, about any sort of exception occurring in plain python app and web.py.
I want something similar to this http://matharvard.ca/posts/2011/jul/31/exception-notification-for-rails-3/
Is there anything same sort present ??
Please reply asap.
Thanks.
You can get what you want by:
Wrapping your code in try..except clause.
Using logging module to log the exceptions with a certain level of severity e.g ERROR.
Setting an SMTPHandler for exceptions of and above certain level.
This way is quite flexible. Your messages can be send to several places (like log files) and you can reconfigure your settings easily.
If you are not using any python heavy weight framework, try: https://github.com/fossilet/exception-notifier , it seems to be similar to the Rails' Exception notification, but quite simple.
If you are using django, seems you can use its built-in feature:https://docs.djangoproject.com/en/dev/howto/error-reporting/ ( and also see this: https://github.com/pinax/django-notification)
If using tornado, try this: https://github.com/kanevski/tornadotoad this is the most similar solution in python comparing to rails. )
You can overwrite the excepthook function from the sys module, and handle any uncought exceptions there.
I have the same requirement with you. I write a simple module to mail uncaught exceptions to the developers, as well as to record them to log files. It is used in our teams' cron scripts written in Python. Hope it can be useful for you too.

Using Django and PyEnchant: Getting MemoryError on shared hosting, but not locally

I'm a beginner level user of Django and Python right now, and so far anything I do locally has immediately worked on my hosting once uploaded. My hosting is provided by Hostmonster.
However, I've just installed PyEnchant. All I use it for is basic spell checking and suggesting new words. Also, 'string' is always a string of words separated by '+'.
from enchant import Dict
def spellcheck(string):
spellcheck = Dict("en-GB")
suggestedword = []
for word in string.split('+'):
if len(word) > 2 and not spellcheck.check(word):
suggestedword.append(spellcheck.suggest(word)[0])
else:
suggestedword.append(word)
return suggestedword
Locally, using the Django dev server, all works fine. On my host I get:
Django Version: 1.4
Exception Type: MemoryError
Exception Location: /home/user/python/lib/python2.7/ctypes/__init__.py in _reset_cache, line 279
It seems to be throwing the error a few steps after 'from enchant import Dict'.
I'm guessing the dictionary is too large to store in temporary memory?
Any idea how to get around this? Please go easy on me if I'm either asking something very stupid, or in a very stupid way :).
If I'm leaving out any vital data, it's because I don't know it's important, so feel free to tell me what other information would help solve this (if it can be solved on a shared host).
Thanks in advance for any help!
EDIT1:
Using SSH, I can import and use PyEnchant:
>>> import enchant
>>> spellcheck = enchant.Dict("en-GB")
>>> spellcheck.suggest('nmae')
['name', 'mane']
Which makes me even more confused, as I have had no luck avoiding 'MemoryError' when I use it as above in my question.
EDIT2:
Still not able to figure this out. If I do 'import enchant' in any module, it seems to cause the MemoryError, yet I am able to use 'import enchant' via remote shell and the python interpreter.
EDIT3:
Still, after a few days of googling and trying things out, I can't get this MemoryError to go away. Has anyone seen this before with 'PyEnchant'? I'm thinking my host is perhaps not giving enough ram to load the PyEnchant import? Is there any way to change how memory is used by a module?
I have just had the same problem after moving my Django installation. The problem was httpd (Apache) access to the database. In my case it was Selinux but I assume that general UNIX type file permissions would cause a similar problem. In this instance it worked fine on the Django server but not on my local Apache when trying out a viable production setup.
Does your host use Linux?
Could you run Apache to help determine the problem?

Where to find python-interpreter error codes?

I have a python application that communicates with a PHP application.
Right now, I have defined error codes on both sides so they match (i.e. we can detect the python-side failures in PHP and react accordingly).
My problem comes from python being an interpreted language, here is why :
If the code has a syntax problem, then the interpreter will return error codes.
But these interpreter error codes will be indistinguishable from the application errors.
I am not in charge of the python code, but I was asked to be able to detect eventual python interpreter failures so I can notify the end user using PHP.
Because of that, I need to find a list of the interpreter return codes when malfunctionning.
I failed to find this information on both google, python doc, and man page.
Does anybody have/know where to find this information ?
(also, if you have any other ideas on how to get around this problem, I'd be happy to hear them)
Thanks in advance !
The best solution would be setting an exception hook that always exits with a certain code.
import sys
def excepthook(type, value, traceback):
sys.exit(X) # X is your exit code
sys.excepthook = excepthook
Syntax errors should be irrelevant - why would you ever put a script that contains syntax errors in production?
But anyway, Python always exits with a non-zero code (apparently always 1) in case of an uncaught exception and 0 if everything went fine.
I believe this might be what you need. It will map the error codes to their respective string messages.
http://docs.python.org/library/errno.html
Specifically:
http://docs.python.org/library/os.html#os.strerror

Where will Python be logging errors for me (moving from PHP)

I am a PHPer new to Python (2.7 on Win32) and I would like to know where Python is shoving any errors it finds?
Do I need to turn something on, if so where do I do that?
Or, is the idea that you develop using a shell and watch errors spat out via that?
Please share any other good Python debugging/sanity-saving mechanisms you wish you'd known about earlier - or if you have switched from PHP perhaps you can tell me what the Python equivalents of :
ini_set('error_reporting', 1);
display_errors();
trigger_error();
var_dump();
Try and Exceptions looks fairly similar.
I will probably stumble across these answers myself in time, but in the meantime this issue is bugging me (no pun intended).
Thanks a lot.
Python development is normally done in a shell, and you get a full traceback printed out on any uncaught exception.
If you want to log errors to file, have a look at the logging module. You can either catch exceptions directly, or override the sys.excepthook function which is called for an uncaught error. If you're using a framework for e.g. web development, it may have mechanisms to do this sort of thing already.

Debugging Django/Python on Dreamhost

Debugging Django on Dreamhost is proving quite the challenge. To my knowledge, print statements aren't available, and neither are logs... any suggestions?
The Django Debug Toolbar, as already mentioned, is damn useful.
But as long as Django is running in debug mode, the brute force method equivalent to the print statement is to simply throw an exception. Put whatever output you want in the exception's text, whenever you need a quick idea of you code's state, and voila... instant output and stacktrace. This isn't a comprehensive solution, but it is a quick print statement style hack.
Have you tried the Django Debug Toolbar?
For a summary of the features, watch the video here.

Categories

Resources