Add error handling in the api