Improved API error handling