Python Debugging with Traceback

I have problem when dealing with chaining function that throws error exception in each method calls like example below.

def func_a():
    try:
        # Error occured in here.
    except ValueError, e:
        raise e

def func_b():
    try:
        func_a()
    except ValueError, e:
        raise e

def func_c():
    try:
        func_b()
    except ValueError, e:
        raise e

if __name__ == '__main__':
    try:
        func_c()
    except Exception, e:
        raise e

From example above, I get used to use ipdb for debugging. Usuallly I find out one by one from func_c() until func_a() to locate the error.

I just found most simple way to deal with it without any extensions. We can use built in function traceback. Traceback will show in which line the error came from without interrupting your program.

Here is example how to use it:

import sys
import traceback

if __name__ == '__main__':
    try:
        func_c()
    except Exception, e:
        traceback.print_exc(file=sys.stdout)
        raise e

And the error stack trace example.

Traceback (most recent call last):
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/views.py", line 155, in create_stpicklist_post_handler
    instance = STPicklist.objects.create(**picklist_meta)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/query.py", line 372, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/models.py", line 210, in save
    super(STPicklist, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/text_processing.py", line 247, in save
    super(ModelDiffMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/text_processing.py", line 207, in save
    super(StripModelMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/base.py", line 589, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/base.py", line 626, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/signals.py", line 119, in signal_create_stactivity
    raise e
InvalidOperation: 0 / 0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s