Skip to content

Commit c03e0ab

Browse files
author
Tjaz Erzen
committed
Handle error for missing api key better
1 parent 8b178fa commit c03e0ab

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

plain2code.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from module_renderer import ModuleRenderer
1818
from plain2code_arguments import parse_arguments
1919
from plain2code_console import console
20-
from plain2code_exceptions import PlainSyntaxError
20+
from plain2code_exceptions import MissingAPIKey, PlainSyntaxError
2121
from plain2code_logger import (
2222
CrashLogHandler,
2323
IndentedFormatter,
@@ -208,6 +208,12 @@ def main():
208208
run_state = RunState(spec_filename=args.filename, replay_with=args.replay_with)
209209

210210
try:
211+
# Validate API key is present
212+
if not args.api_key:
213+
raise MissingAPIKey(
214+
"API key is required. Please set the CODEPLAIN_API_KEY environment variable or provide --api-key argument."
215+
)
216+
211217
render(args, run_state, codeplain_api, event_bus)
212218
except InvalidFridArgument as e:
213219
console.error(f"Error rendering plain code: {str(e)}.\n")
@@ -237,6 +243,8 @@ def main():
237243
console.error(f"Error rendering plain code: {str(e)}\n")
238244
console.debug(f"Render ID: {run_state.render_id}")
239245
dump_crash_logs(args)
246+
except MissingAPIKey as e:
247+
console.error(f"Missing API key: {str(e)}\n")
240248
except Exception as e:
241249
console.error(f"Error rendering plain code: {str(e)}\n")
242250
console.debug(f"Render ID: {run_state.render_id}")

plain2code_arguments.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
from plain2code_read_config import get_args_from_config
66

77
CODEPLAIN_API_KEY = os.getenv("CODEPLAIN_API_KEY")
8-
if not CODEPLAIN_API_KEY:
9-
CLAUDE_API_KEY = os.getenv("CLAUDE_API_KEY")
10-
if not CLAUDE_API_KEY:
11-
raise ValueError("CODEPLAIN_API_KEY or CLAUDE_API_KEY environment variable is not set")
128

139

1410
DEFAULT_BUILD_FOLDER = "plain_modules"

plain2code_exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ class MultipleRendersFound(Exception):
4343

4444
class UnexpectedState(Exception):
4545
pass
46+
47+
48+
class MissingAPIKey(Exception):
49+
pass

0 commit comments

Comments
 (0)