Skip to content

Commit

Permalink
fix(api): add error handling for OpenAI API failures
Browse files Browse the repository at this point in the history
  • Loading branch information
zqqian committed Feb 12, 2025
1 parent fece247 commit 2e2e878
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ def do_translate(self, text) -> str:
**self.options,
messages=self.prompt(text, self.prompttext),
)
if not response.choices:
if hasattr(response, "error"):
raise ValueError("Empty response from OpenAI API", response.error)
else:
raise ValueError("Empty response from OpenAI API")
return response.choices[0].message.content.strip()

def get_formular_placeholder(self, id: int):
Expand Down

0 comments on commit 2e2e878

Please sign in to comment.