Help - Infinite Loop: Error in trap function

Hi,

I was working on implementing error handling in my bash scripts, and decided to use trap to send myself an email incase of any errors. But it seems that somethings has gone wrong, and I am continuously getting same emails for an old error repeatedly (even though I have stopped/killed all instances of the script).

Artifacts:
I am using a common script called: catch-errors.sh to catch errors:

catch-errors.sh

#!/bin/bash

set -o pipefail  # trace ERR through pipes
set -o errtrace  # trace ERR through 'time command' and other functions
set -o nounset   ## set -u : exit the script if you try to use an uninitialised variable
set -o errexit

function error_exit {
  LINE_NO=$2
  echo "`date` - Script $1 failed at line:${LINE_NO} with error-code:$3" | mail -s "Application XYZ failure" my-email-id@domain.com -- -f SenderName
  exit 2
}
PROG_NAME=$0
trap 'error_exit ${PROG_NAME} ${LINENO} $?' SIGHUP SIGINT SIGTERM ERR

</code>

Now, I am using above script in another main script for error handling:
main-script.sh

#!/bin/bash

. ./catch-error.sh

COUNT=0

while [ $COUNT -lt 10 ]
do
  --- do something --
  COUNT=$((COUNT+1))
done

echo "script completed."

Can someone please help.. how to stop the emails I am continuously getting?
I have killed any instance of above script, but still getting emails.
I have observed one thing - all the emails are for an old error since the timestamp being printed is the same old one, so I am guessing that maybe the trap function itself is throwing some error which is causing it to invoke itself again and again.

Wed Feb 25 15:07:31 PST 2015 - Script XYZ failed at line:16 with error-code:1

Any help would be appreciated..

Hi Aqua

To my understanding, the issue is behind the --- do something --- .
Though, havent worked with trap yet.

Not sure if you could 'add' a break to the trap, as in, break if trap gets raised?
(EDIT: just seeing now...) Confusing part is, you already got an exit 2 there....

hth