Return Error object for failed Api requests and fixed flaky test.