better error handling in API