Capturing error codes in SFTP commands

Hi,
I have been using the SFTP commands in my reusable shell scripts to perform Get/Put operation. The script has a list of 6 errors which i am capturing through the log file using grep command.

But I feel there might me more errors which the script might need to capture. I tried to capture the error code in the below script but its not helping me.
I had seen many forums where it was suggested to install password authentication and run the sftp commands through batch mode to capture the error codes dynamically. As this script is to go live in a couple of days, it won't be possible for me to install the password authentication now.

Can anyone suggest how to capture the error code for the below commands?
The inputs for the sftp commands are parameterized.

  
  /usr/local/bin/expect 2>&1 >> ${Log_File_Name} <<EOF
  set timeout 200
  spawn /usr/bin/sftp $Ftpusr@$FtpSrvrNme
  expect "assword:"
  send "$(</path/password.usr)\n"
  expect "sftp>"
  send "cd ${Dir}\r"
  expect "sftp>"
  send "pwd\r"
  expect "sftp>"
  send "${FtpAction} ${FileName}\r"
  expect "sftp>"
  send "bye\r"
EOF

Try replacing

/usr/local/bin/expect 2>&1 >> ${Log_File_Name} <<EOF

with

 /usr/local/bin/expect <<EOF  >> ${Log_File_Name} 2>&1

First of all, and for lots of good reasons, might I suggest generating SSH-keys and setting up password-less authentication. This way you can dispose of expect and remove the risk of this code being read and someone else finding your credentials in plain text.

Additionally, this will ease your requirement. If you put the sftp subcommands you want into a batch file, you can call it like this:-

sftp -b batch_file user@server >logfile 2>&1

Should errors occur, the sftp should exit with a non-zero return code which you can easily test for.

I hope that this helps,
Robin