improve error handling in api and api factory