code refactor to handle error and toast in one common place