Skip to content

Commit 7bb058f

Browse files
committed
Add CardGeneratorError class
1 parent 52d3a4b commit 7bb058f

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

karten/card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class CardError(Exception):
9-
"""Card could not be created"""
9+
"""Card parsing failed"""
1010

1111

1212
class Card(BaseModel):

karten/card_generator.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
from google import genai
66
from google.genai import types
77

8-
from .card import Card, CardError
8+
from .card import Card
99
from .prompt import CardPrompt
1010

1111

12+
class CardGeneratorError(Exception):
13+
"""Card generation failed"""
14+
15+
1216
class CardGenerator:
1317
"""Generates flashcards using Google GenAI"""
1418

1519
def __init__(self, api_key: str, model_name: str):
1620
if not api_key:
17-
raise CardError("API key must be provided")
21+
raise CardGeneratorError("API key must be provided")
1822
self.client = genai.Client(api_key=api_key)
1923
self.model_name = model_name
2024
self.config = types.GenerateContentConfig(
@@ -34,7 +38,7 @@ def generate_content(self, prompt: str) -> str:
3438
config=self.config,
3539
)
3640
if response.text is None:
37-
raise CardError("No response text from model")
41+
raise CardGeneratorError("No response text from model")
3842
return response.text
3943

4044
def collect(self, word: str, lang: str) -> Card:

karten/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from . import __version__
99
from .card import CardError
10-
from .card_generator import CardGenerator
10+
from .card_generator import CardGenerator, CardGeneratorError
1111
from .cli_options import (
1212
option_date_from,
1313
option_file,
@@ -36,8 +36,8 @@ def card(word, lang, key, model):
3636
generator = CardGenerator(api_key=key, model_name=model)
3737
card = generator.collect(word, lang) # pylint: disable=redefined-outer-name
3838
click.echo(card.model_dump_json(indent=2, exclude_none=True))
39-
except CardError as e:
40-
click.echo(e)
39+
except (CardError, CardGeneratorError) as e:
40+
click.echo(f"Error: {e}")
4141

4242

4343
@cli.command()
@@ -76,7 +76,7 @@ def _create_deck(
7676

7777
try:
7878
generator = CardGenerator(api_key=key, model_name=model_name)
79-
except CardError as e:
79+
except CardGeneratorError as e:
8080
click.echo(f"Error: {e}")
8181
return
8282

0 commit comments

Comments
 (0)