forked from kenzuya/unshackle
fix(api): let a core failure name its own API error code
This commit is contained in:
@@ -13,6 +13,7 @@ import requests
|
||||
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.remote_protocol import APIErrorCode, coded
|
||||
from unshackle.core.utilities import get_country_code, get_country_name, get_debug_logger, log_event
|
||||
from unshackle.core.utils import run_proxy
|
||||
from unshackle.core.utils.ip_info import get_ip_info
|
||||
@@ -191,7 +192,7 @@ class Gluetun(Proxy):
|
||||
verify_ip: Automatically make sure that the IP and region are correct after connection (default: True)
|
||||
"""
|
||||
if not binaries.Docker:
|
||||
raise RuntimeError("Docker not found, the Gluetun proxy needs it")
|
||||
raise coded(RuntimeError("Docker not found, the Gluetun proxy needs it"), APIErrorCode.INVALID_PROXY)
|
||||
|
||||
self.providers = providers or {}
|
||||
self.base_port = base_port
|
||||
@@ -365,7 +366,7 @@ class Gluetun(Proxy):
|
||||
self.remove_container(container_name)
|
||||
if query_key in self.active_containers:
|
||||
del self.active_containers[query_key]
|
||||
raise RuntimeError(f"Failed to create Gluetun container: {e}")
|
||||
raise coded(RuntimeError(f"Failed to create Gluetun container: {e}"), APIErrorCode.INVALID_PROXY)
|
||||
|
||||
def cleanup(self) -> None:
|
||||
"""Stop and remove all managed Gluetun containers."""
|
||||
|
||||
@@ -8,6 +8,7 @@ import requests
|
||||
|
||||
from unshackle.core.proxies.asn import describe, is_ip, mapped_query, select, split_selector
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
from unshackle.core.remote_protocol import APIErrorCode, coded
|
||||
from unshackle.core.utilities import COUNTRY_CODE_ALIASES
|
||||
|
||||
|
||||
@@ -24,7 +25,10 @@ class NordVPN(Proxy):
|
||||
if not password:
|
||||
raise ValueError("No password for the NordVPN proxy provider")
|
||||
if not re.match(r"^[a-z0-9]{48}$", username + password, re.IGNORECASE) or "@" in username:
|
||||
raise ValueError("NordVPN needs its Service Credentials, not your login credentials")
|
||||
raise coded(
|
||||
ValueError("NordVPN needs its Service Credentials, not your login credentials"),
|
||||
APIErrorCode.INVALID_PROXY,
|
||||
)
|
||||
|
||||
if server_map is not None and not isinstance(server_map, dict):
|
||||
raise TypeError(f"Expected server_map to be a dict mapping a region to a server ID, not '{server_map!r}'.")
|
||||
|
||||
@@ -14,6 +14,7 @@ import requests
|
||||
from click.core import ParameterSource
|
||||
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.remote_protocol import APIErrorCode, coded
|
||||
from unshackle.core.utils.redact import mask_proxy
|
||||
|
||||
log = logging.getLogger("proxies")
|
||||
@@ -46,12 +47,15 @@ def find_provider(proxy_providers: List[Any], name: str, hint: str = "") -> Any:
|
||||
for provider in proxy_providers:
|
||||
if isinstance(provider, Unavailable):
|
||||
if provider.name.lower() == name.lower():
|
||||
raise ValueError(f"The proxy provider {provider.name} could not load: {provider.error}")
|
||||
raise coded(
|
||||
ValueError(f"The proxy provider {provider.name} could not load: {provider.error}"),
|
||||
APIErrorCode.INVALID_PROXY,
|
||||
)
|
||||
elif provider.__class__.__name__.lower() == name.lower():
|
||||
return provider
|
||||
available = [x.__class__.__name__ for x in proxy_providers if not isinstance(x, Unavailable)]
|
||||
message = f"The proxy provider '{name}' was not found. Available: {available}"
|
||||
raise ValueError(f"{message}. {hint}" if hint else message)
|
||||
raise coded(ValueError(f"{message}. {hint}" if hint else message), APIErrorCode.INVALID_PROXY)
|
||||
|
||||
|
||||
def load_proxy_providers(exclude: tuple[str, ...] = (), raise_errors: bool = False, quiet: bool = False) -> List[Any]:
|
||||
@@ -166,9 +170,13 @@ def pick_proxy(proxy_providers: List[Any], query: str, provider_name: Optional[s
|
||||
except Exception as e:
|
||||
if isinstance(e, ValueError) and not isinstance(e, (json.JSONDecodeError, requests.RequestException)):
|
||||
raise
|
||||
raise ValueError(f"The proxy provider {provider_name} failed for {query}: {e}") from e
|
||||
raise coded(
|
||||
ValueError(f"The proxy provider {provider_name} failed for {query}: {e}"), APIErrorCode.INVALID_PROXY
|
||||
) from e
|
||||
if not uri:
|
||||
raise ValueError(f"The proxy provider {provider_name} had no proxy for {query}")
|
||||
raise coded(
|
||||
ValueError(f"The proxy provider {provider_name} had no proxy for {query}"), APIErrorCode.INVALID_PROXY
|
||||
)
|
||||
return provider, uri
|
||||
errors = []
|
||||
for provider in proxy_providers:
|
||||
@@ -182,7 +190,7 @@ def pick_proxy(proxy_providers: List[Any], query: str, provider_name: Optional[s
|
||||
if uri:
|
||||
return provider, uri
|
||||
detail = f" ({'; '.join(errors)})" if errors else ""
|
||||
raise ValueError(f"No proxy provider had a proxy for {query}{detail}")
|
||||
raise coded(ValueError(f"No proxy provider had a proxy for {query}{detail}"), APIErrorCode.INVALID_PROXY)
|
||||
|
||||
|
||||
def describe_proxy(provider: Any, uri: str, query: str, allow_debug: bool = True) -> str:
|
||||
|
||||
@@ -15,6 +15,7 @@ import requests
|
||||
from unshackle.core.proxies.asn import describe, fold, is_ip, mapped_query, select, split_selector
|
||||
from unshackle.core.proxies.controld import build_query, parse_a_records
|
||||
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
|
||||
from unshackle.core.remote_protocol import APIErrorCode, coded
|
||||
from unshackle.core.utilities import COUNTRY_CODE_ALIASES
|
||||
|
||||
log = logging.getLogger("proxies.surfsharkvpn")
|
||||
@@ -95,7 +96,10 @@ class SurfsharkVPN(Proxy):
|
||||
if not password:
|
||||
raise ValueError("No password for the SurfsharkVPN proxy provider")
|
||||
if not re.match(r"^[a-z0-9]{48}$", username + password, re.IGNORECASE) or "@" in username:
|
||||
raise ValueError("SurfsharkVPN needs its Service Credentials, not your login credentials")
|
||||
raise coded(
|
||||
ValueError("SurfsharkVPN needs its Service Credentials, not your login credentials"),
|
||||
APIErrorCode.INVALID_PROXY,
|
||||
)
|
||||
|
||||
if server_map is not None and not isinstance(server_map, dict):
|
||||
raise TypeError(
|
||||
|
||||
Reference in New Issue
Block a user