Make api error extraction logic more readable