By default, the ERR
trap isn't inherited by shell functions.
Quoting from help set
:
-E If set, the ERR trap is inherited by shell functions.
-o option-name
Set the variable corresponding to option-name:
errtrace same as -E
Saying
set -o errtrace
at the beginning of your script should make it work as you expect.