Add smarter handling of ES errors

http://manage.dimagi.com/default.asp?106737#603620