mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-07-27 23:10:27 +08:00
fix(proxy): raise ValueError for unknown IP_PROXY_PROVIDER_NAME
When IP_PROXY_PROVIDER_NAME in config.py is set to an unrecognised value (e.g. typo "kuaidalli" instead of "kuaidaili"), IpProxyProvider.get() returns None which is silently stored in ProxyIpPool.ip_provider. The crash surfaces later as: AttributeError: 'NoneType' object has no attribute 'get_proxy' with no hint about what went wrong or how to fix it. Add an explicit None-check in create_ip_pool() that raises ValueError with the bad name and the list of valid options, so the root cause is immediately visible. Also add a regression test covering the four invalid-name edge cases (typo, empty string, wrong case, unknown name) and the three valid names to confirm they are unaffected.
This commit is contained in:
@@ -202,11 +202,17 @@ async def create_ip_pool(ip_pool_count: int, enable_validate_ip: bool) -> ProxyI
|
||||
:param enable_validate_ip: Whether to enable IP proxy validation
|
||||
:return:
|
||||
"""
|
||||
ip_provider = IpProxyProvider.get(config.IP_PROXY_PROVIDER_NAME)
|
||||
if ip_provider is None:
|
||||
raise ValueError(
|
||||
f"Unknown proxy provider: '{config.IP_PROXY_PROVIDER_NAME}'. "
|
||||
f"Valid options: {list(IpProxyProvider.keys())}"
|
||||
)
|
||||
is_static = config.IP_PROXY_PROVIDER_NAME == ProviderNameEnum.STATIC_PROVIDER.value
|
||||
pool = ProxyIpPool(
|
||||
ip_pool_count=ip_pool_count,
|
||||
enable_validate_ip=False if is_static else enable_validate_ip,
|
||||
ip_provider=IpProxyProvider.get(config.IP_PROXY_PROVIDER_NAME),
|
||||
ip_provider=ip_provider,
|
||||
)
|
||||
await pool.load_proxies()
|
||||
return pool
|
||||
|
||||
Reference in New Issue
Block a user