OpenAI has updated their API, and the openai.error.OpenAIError no longer exists. You need to modify it to openai.OpenAIError for the code to work. However, making this change will trigger a warning, which recommend to downgrade the version to below 0.28.
AttributeError: module ‘openai’ has no attribute ‘error’