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}" 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) 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") 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: with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit( future = executor.submit(
@ -69,8 +66,6 @@ class TorModule:
"The dedicated Tor service could not be initialized." "The dedicated Tor service could not be initialized."
) )
# if connection_observer is not None:
# connection_observer.notify("tor_bootstrapped")
print("tor bootstrapped") print("tor bootstrapped")
try: try:
@ -79,12 +74,15 @@ class TorModule:
) )
controller.authenticate() 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() self.stop_service()
raise TorServiceInitializationError( return False
"The dedicated Tor service could not be initialized."
)
def stop_service(self): def stop_service(self):
control_socket_file = Path(self.control_socket_path) control_socket_file = Path(self.control_socket_path)
@ -134,7 +132,7 @@ class TorModule:
except (FileNotFoundError, stem.SocketError, TypeError, IndexError): 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( controller = stem.control.Controller.from_socket_file(
self.control_socket_path self.control_socket_path
@ -148,11 +146,11 @@ class TorModule:
controller.set_conf("SocksPort", socks_port_numbers) controller.set_conf("SocksPort", socks_port_numbers)
def destroy_session(self, port_number: int): def destroy_session(self, port_number: int):
print("destroying Tor session") print("destroying Tor session")
try: try:
controller = stem.control.Controller.from_socket_file( controller = stem.control.Controller.from_socket_file(
self.control_socket_path self.control_socket_path
) )
@ -163,19 +161,21 @@ class TorModule:
] ]
if len(socks_port_numbers) > 1: if len(socks_port_numbers) > 1:
socks_port_numbers = [ socks_port_numbers = [
socks_port_number socks_port_number
for socks_port_number in socks_port_numbers for socks_port_number in socks_port_numbers
if socks_port_number != port_number if socks_port_number != port_number
] ]
controller.set_conf("SocksPort", socks_port_numbers) controller.set_conf("SocksPort", socks_port_numbers)
else: else:
controller.set_conf("SocksPort", "0") controller.set_conf("SocksPort", "0")
except (FileNotFoundError, stem.SocketError, TypeError, IndexError): except (FileNotFoundError, stem.SocketError, TypeError, IndexError) as e:
pass 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 @staticmethod
def __on_initialization_message( def __on_initialization_message(

View file

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

View file

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