Friday, October 21, 2011

IndexError: tuple index out of range in get_template_exception_info

Sometimes people do not specify messages, when they're firing exceptions. In that case, one day we can get something like this in http server stdout:
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/core/handlers/base.py", line 221, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 66, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 275, in get_traceback_html
    c = Context(self.get_traceback_data())
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 231, in get_traceback_data
    self.get_template_exception_info()
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 306, in get_template_exception_info
    'message': self.exc_value.args[0],
IndexError: tuple index out of range
And in a browser
A server error has occurred. Please contact the administrator
Quick workaround
  1. Open /Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py(use your own path from error trace) in your favourite editor
  2. Goto line 306, find this text
  3.     'message': self.exc_value.args[0],
    
  4. Replace it with
  5.     'message': self.exc_value.args[0] if self.exc_value.args else None,
    
Now you can restart http server(if it wasn't done automagically) and see detailed debug information.

Thursday, October 20, 2011

tddspry, twill and UnicodeDecodeError

Problem was caused by this naive code, which tries to find string "http://www.fedex.com/Tracking?cntry_code=us&tracknumber_list=123465789&language=english" in plain HTML
class ShipmentsListTest(DatabaseTestCase):
    def test_tracking_link_presence(self):
        self.go200('members:shipments_list')
        self.find(self.rentorder.outgoing_tracking_link, flat=True, escape=True)
Which causes this:
  File "/Users/zomg/.virtualenvs/dev/src/tddspry/tddspry/django/cases.py", line 497, in find
    real_count = html.count(what)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 277: ordinal not in range(128)
Follownig tddspry code we can found that on escape flag it applies django escape method, which returns an unicode string.
from django.utils.html import escape as real_escape
.. skipped ..

def find(self, what, flags='', flat=False, count=None, escape=False):
    if escape:
        what = real_escape(what)

Unfortunately, in twill won't work with unicode strings. So here is quick workaround, make string escaping manually and force it to string.
from django.utils.html import escape as real_escape

class ShipmentsListTest(DatabaseTestCase):
    def test_tracking_link_presence(self):
        self.go200('members:shipments_list')
        link = str(real_escape(self.rentorder.outgoing_tracking_link))
        self.find(link, flat=True)

Voila!

Wednesday, October 19, 2011

How to clear SUDS cache

At suds/cache.py we can follow the way how is the cache path is generated.
    if location is None:
            location = os.path.join(tmp(), 'suds')
        log.debug("cache dir is %s" % location)
        self.location = location
Here is the code that wipes all chache data in default place.
import os
import shutil
from tempfile import gettempdir as tmp
shutil.rmtree(os.path.join(tmp(), 'suds'), True)

Tuesday, May 17, 2011

One of reason's Facebook's API 'Error validating verification code.' at authentication process

Gist:
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream


https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE


YOUR_URL must be same on both requests.

Prose:
I have little service which used to be social platform(twttr,fb,myspace).

Today i've spent some time on fixing bug, in my implementation of server side facebook auth flow. Probably I didn't understand well strictness of flow, so I went in struggle with :

{
"error": {
"type": "OAuthException",
"message": "Error validating verification code."
}
}

Experimental way i've found that i've changed redirect_uri param on second step, so it was different with first request. Which wasn't obvious for me :].

Actually i don't understand why we need redirect_uri param in request which goes directly from script to facebook server, without user involving.

Sunday, April 17, 2011

ipython for ruby

Auto-completion, coloring with irb huang47.blogspot.com/2010/02/irb-auto-complete-with-syntax-highlight.html

Thursday, March 24, 2011

Get list of all databases in PostgreSQL

In mySQL we use following query to get the list:
SHOW DATABASES ;.
In PostgreSQL we should use :
SELECT datname FROM pg_database;