Skip to content

Commit

Permalink
fix(backends): Add error message if dependency for BE not found
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousLearner committed Oct 13, 2024
1 parent bc98813 commit 4e35b52
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions phone_verify/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ def get_sms_backend(phone_number):
if not backend:

if settings.PHONE_VERIFICATION.get("BACKEND", None):
backend_import = settings.PHONE_VERIFICATION["BACKEND"]
backend_import_path = settings.PHONE_VERIFICATION["BACKEND"]
else:
raise ImproperlyConfigured(
"Please specify BACKEND in PHONE_VERIFICATION within your settings"
)

backend_cls = import_string(backend_import)
try:
backend_cls = import_string(backend_import_path)
except ImportError as e:
if not any(provider in backend_import_path.lower() for provider in ['twilio', 'nexmo']):
# Error for custom backends
raise RuntimeError(f"Failed to import the specified backend: {backend_import_path}. Ensure the module is installed and properly configured.") from e

# Extract the module name (e.g., 'twilio' or 'nexmo') for a more meaningful error message
dependency_name = backend_import_path.split(".")[-2]

# Raise an error with the correct dependency name
raise RuntimeError(f"{dependency_name.capitalize()} backend is not installed. Please install '{dependency_name}' to use this provider.") from e

return backend_cls(**settings.PHONE_VERIFICATION["OPTIONS"])

0 comments on commit 4e35b52

Please sign in to comment.