I just fixed almost exactly the same situation. Hope it still helps you, if not then perhaps others. I started from su, not sudo, but since sudo is intended to wrap a single other command, it really ought to relay su's exit code. If not, you can apply the fix below on the sudo level also.
Like you noted the main problem is that su succesfully executes it's command. The default action is then to report that it completed without any problems and so it returns exit code 0. It doesn't "know" that a non 0 exit code from a command was unexpected, or that it should do something with it. Thus the solution is quite simply to make su return the exit code of it's last command. This did it for me
su <user_x> -c '<bunch_of_commands>; exit $?'
In case sudo doesn't play nice, the whole command should be something like this (I would check this for you but I don't have sudo installed)
sudo 'su <user_x> -c \'<bunch_of_commands>; exit $?\'; exit$?'
Watch for the nesting of quotes and make sure $? doesn't get expanded, so no double quotes.