Improve error handling.

Change-Id: I13f7949e220c966b40d72a473a35861128d07914