feat(devgen): enhance provider import error handling
Enhancements: - Split ModuleNotFoundError and AttributeError handling to deliver clearer diagnostics - Explicitly list available providers and flag missing provider classes as packaging bugs Bug Fixes: - Provide specific guidance when a provider module is not a built‑in module - Clarify that a missing provider class indicates a packaging issue rather than user error
This commit is contained in:
1 file changed
+10
-2
+10
-2
@@ -21,8 +21,16 @@ def generate_with_ai(
|
||||
provider_module = import_module(f"devgen.providers.{provider}")
|
||||
class_name = "".join([x.capitalize() for x in provider.split("_")]) + "Provider"
|
||||
provider_class = getattr(provider_module, class_name)
|
||||
except (ModuleNotFoundError, AttributeError) as e:
|
||||
raise ImportError(f"Provider `{provider}` not found or invalid: {e}") from e
|
||||
except ModuleNotFoundError as e:
|
||||
raise ImportError(
|
||||
f"Provider `{provider}` is not a built-in module: {e}. "
|
||||
f"Available providers: gemini, openai, anthropic, huggingface, openrouter."
|
||||
) from e
|
||||
except AttributeError as e:
|
||||
raise ImportError(
|
||||
f"Provider `{provider}` exists but does not expose a "
|
||||
f"`{class_name}` class. This is a packaging bug."
|
||||
) from e
|
||||
|
||||
provider_instance = provider_class()
|
||||
return provider_instance.generate(prompt, api_key=api_key, model=model, **kwargs)
|
||||
Reference in new issue
Block a user