Refactored api_error check to one-level if-else.