2121 ConflictingRequirements ,
2222 CreditBalanceTooLow ,
2323 InternalServerError ,
24+ InvalidAPIKey ,
2425 InvalidFridArgument ,
2526 LLMInternalError ,
2627 MissingAPIKey ,
2728 MissingPreviousFunctionalitiesError ,
2829 MissingResource ,
30+ OutdatedClientVersion ,
2931 PlainSyntaxError ,
3032 UnexpectedState ,
3133)
@@ -163,6 +165,24 @@ def setup_logging(
163165 root_logger .info (f"Render ID: { render_id } " ) # Ensure render ID is logged in to codeplain.log file
164166
165167
168+ def _check_connection (codeplainAPI ):
169+ """Check API connectivity and validate API key and client version."""
170+ response = codeplainAPI .connection_check (system_config .client_version )
171+
172+ if not response .get ("api_key_valid" , False ):
173+ raise InvalidAPIKey (
174+ "Please provide a valid API key using the CODEPLAIN_API_KEY environment variable "
175+ "or the --api-key argument."
176+ )
177+
178+ if not response .get ("client_version_valid" , False ):
179+ min_version = response .get ("min_client_version" , "unknown" )
180+ raise OutdatedClientVersion (
181+ f"Your client version ({ system_config .client_version } ) is outdated. Minimum required version is { min_version } . "
182+ "Please update using: uv tool upgrade codeplain"
183+ )
184+
185+
166186def render (args , run_state : RunState , codeplain_api , event_bus : EventBus ): # noqa: C901
167187 # Check system requirements before proceeding
168188 system_config .verify_requirements ()
@@ -187,6 +207,8 @@ def render(args, run_state: RunState, codeplain_api, event_bus: EventBus): # no
187207 assert args .api is not None and args .api != "" , "API URL is required"
188208 codeplainAPI .api_url = args .api
189209
210+ _check_connection (codeplainAPI )
211+
190212 module_renderer = ModuleRenderer (
191213 codeplainAPI ,
192214 args .filename ,
@@ -266,6 +288,10 @@ def main(): # noqa: C901
266288 console .debug (f"Render ID: { run_state .render_id } " )
267289 except MissingAPIKey as e :
268290 console .error (f"Missing API key: { str (e )} \n " )
291+ except InvalidAPIKey as e :
292+ console .error (f"Invalid API key: { str (e )} \n " )
293+ except OutdatedClientVersion as e :
294+ console .error (f"Outdated client version: { str (e )} \n " )
269295 except (InternalServerError , UnexpectedState ):
270296 exc_info = sys .exc_info ()
271297 console .error (
0 commit comments