Improved exception error handling

This commit is contained in:
SimplifiedPrivacy 2026-07-12 17:27:04 -04:00
parent 8f0f511a2a
commit 78ff72b5d8
3 changed files with 18 additions and 17 deletions

View file

@ -31,7 +31,7 @@ class TorModule:
f"{self.state_home}/{Constants.TOR_INSTANCE_LOCK_FILENAME}"
)
def start_service(self, connection_observer: Optional[ConnectionObserver] = None):
def start_service(self, connection_observer: Optional[ConnectionObserver] = None) -> bool:
Path(self.state_home).mkdir(mode=0o700, parents=True, exist_ok=True)
@ -40,9 +40,6 @@ class TorModule:
print("[TOR MODULE] Starting Tor service without IPv6")
# if connection_observer is not None:
# connection_observer.notify("tor_bootstrapping")
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(
@ -69,8 +66,6 @@ class TorModule:
"The dedicated Tor service could not be initialized."
)
# if connection_observer is not None:
# connection_observer.notify("tor_bootstrapped")
print("tor bootstrapped")
try:
@ -79,12 +74,15 @@ class TorModule:
)
controller.authenticate()
except (FileNotFoundError, stem.SocketError, TypeError, IndexError):
return True
except (FileNotFoundError, stem.SocketError, TypeError, IndexError) as e:
print(f"Tor startup failed: {e}")
if connection_observer:
connection_observer.notify("error", f"Tor startup failed {e}")
self.stop_service()
raise TorServiceInitializationError(
"The dedicated Tor service could not be initialized."
)
return False
def stop_service(self):
control_socket_file = Path(self.control_socket_path)
@ -134,7 +132,7 @@ class TorModule:
except (FileNotFoundError, stem.SocketError, TypeError, IndexError):
self.start_service(connection_observer=connection_observer)
service_begun = self.start_service(connection_observer=connection_observer)
controller = stem.control.Controller.from_socket_file(
self.control_socket_path
@ -148,11 +146,11 @@ class TorModule:
controller.set_conf("SocksPort", socks_port_numbers)
def destroy_session(self, port_number: int):
print("destroying Tor session")
try:
controller = stem.control.Controller.from_socket_file(
self.control_socket_path
)
@ -163,19 +161,21 @@ class TorModule:
]
if len(socks_port_numbers) > 1:
socks_port_numbers = [
socks_port_number
for socks_port_number in socks_port_numbers
if socks_port_number != port_number
]
controller.set_conf("SocksPort", socks_port_numbers)
else:
controller.set_conf("SocksPort", "0")
except (FileNotFoundError, stem.SocketError, TypeError, IndexError):
pass
except (FileNotFoundError, stem.SocketError, TypeError, IndexError) as e:
error_type = e.__class__.__name__
print(f"destroy_session failed: {error_type}: {str(e)}")
# if connection_observer:
# connection_observer.notify("error", f"Tor destroy_session: {error_type}")
@staticmethod
def __on_initialization_message(

View file

@ -8,3 +8,4 @@ class ConnectionObserver(BaseObserver):
self.on_tor_bootstrapping = []
self.on_tor_bootstrap_progressing = []
self.on_tor_bootstrapped = []
self.on_custom_message = []

View file

@ -1,6 +1,6 @@
[project]
name = "sp-essentials"
version = "1.2.0"
version = "1.2.1"
authors = [
{ name = "Simplified Privacy" },
]