Corrected error handling code after API change