added error handling to api