added uncaught exception handling and write error to mongo collection