Better error handling for API