Added error handling when calling endpoint