Skip to content

Commit 4e35b52

Browse files
fix(backends): Add error message if dependency for BE not found
1 parent bc98813 commit 4e35b52

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

phone_verify/backends/__init__.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@ def get_sms_backend(phone_number):
1212
if not backend:
1313

1414
if settings.PHONE_VERIFICATION.get("BACKEND", None):
15-
backend_import = settings.PHONE_VERIFICATION["BACKEND"]
15+
backend_import_path = settings.PHONE_VERIFICATION["BACKEND"]
1616
else:
1717
raise ImproperlyConfigured(
1818
"Please specify BACKEND in PHONE_VERIFICATION within your settings"
1919
)
2020

21-
backend_cls = import_string(backend_import)
21+
try:
22+
backend_cls = import_string(backend_import_path)
23+
except ImportError as e:
24+
if not any(provider in backend_import_path.lower() for provider in ['twilio', 'nexmo']):
25+
# Error for custom backends
26+
raise RuntimeError(f"Failed to import the specified backend: {backend_import_path}. Ensure the module is installed and properly configured.") from e
27+
28+
# Extract the module name (e.g., 'twilio' or 'nexmo') for a more meaningful error message
29+
dependency_name = backend_import_path.split(".")[-2]
30+
31+
# Raise an error with the correct dependency name
32+
raise RuntimeError(f"{dependency_name.capitalize()} backend is not installed. Please install '{dependency_name}' to use this provider.") from e
33+
2234
return backend_cls(**settings.PHONE_VERIFICATION["OPTIONS"])

0 commit comments

Comments
 (0)