This is the error I want to catch:
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/opt/django/fileupload/views.py" in upload_file
56. folder_info = url.split(':')[1] #Extracts folder info to use in the header
Exception Type: IndexError at /upload_file/
Exception Value: list index out of range
Tried using:
except (socket.error, paramiko.AuthenticationException, IndexError):
return render_to_response('reform.html', context_instance=RequestContext(request))
But it doesn’t work.
I am able to catch the socket.error and paramiko.Authentication exceptions but not the IndexError. I am trying to catch the exception in Django. Thanks.
Edit:
Entire try and except block:
try:
source = str(username) + "@" + url #Source to list all the files
add_key = str(username) + "@" + test_url
add_known_hosts(password, add_key) #Add to the known hosts
test_ssh(test_url, username, password) #Test Host_name, username and password
destination = '/home/sachet/files'
command = subprocess.Popen(['sshpass', '-p', password, 'rsync', '--recursive', source],
stdout=subprocess.PIPE).communicate()[0] #sshpass needs to be installed into the server
lines = (x.strip() for x in command.split('\n'))
remote = [x.split(None, 4)[-1] for x in lines if x] #Removes permission from the file listing
base_name = [os.path.basename(ok) for ok in remote]
result = subprocess.Popen(['ls', destination], stdout=subprocess.PIPE).communicate()[0].splitlines()
return render_to_response('thanks.html', {'res1': remote, 'res': result, 'folder': folder_info}, context_instance=RequestContext(request))
except (socket.error, paramiko.AuthenticationException, IndexError):
return render_to_response('reform.html', context_instance=RequestContext(request))
The IndexError is not raised within the try/except block you posted. The IndexError is raised on the line where folder_info is assigned to (line 56 in views.py). You need to move that line of code into your try/except block in order to catch that error.
Or, better yet, put a separate try/except around the “folder = …” line, just for the IndexError, to make the intention of your code clearer.