Isolated error translation to it's own module. Finality to ticket payments and the ticket prep changing the Config

This commit is contained in:
SimplifiedPrivacy 2026-10-17 17:13:42 -04:00
parent 9ca0809ceb
commit 5023b09928
5 changed files with 116 additions and 7 deletions

View file

@ -0,0 +1,81 @@
from core.controllers.ConfigurationController import ConfigurationController
from core.models.Result import Result, ResultError
from core.errors.logger import logger
from core.Errors import (
CommandNotFoundError, EndpointVerificationError, FileIntegrityError,
InvalidSubscriptionError, MissingSubscriptionError, ProfileActivationError,
ProfileModificationError, ProfileStateConflictError, UnsupportedApplicationVersionError,
)
from typing import Type, Optional
ERROR_TO_UI_MESSAGE = {
ProfileActivationError: "The profile could not be enabled",
UnsupportedApplicationVersionError: "The application version in question is not supported",
FileIntegrityError: "Application version file integrity could not be verified.",
ProfileModificationError: "WireGuard configuration could not be attached.",
}
def translate_error(error: Type[Exception], profile_id: int) -> Result:
"""Translate legacy core errors into actions and/or messages via Result Objects."""
logger.error(str(error))
#----------------------
# PURE MESSAGES (NO SOLUTION FUNCTION)
#----------------------
if error in ERROR_TO_UI_MESSAGE:
error_msg = ERROR_TO_UI_MESSAGE[error]
return Result(valid=False, error_type=ResultError.UNKNOWN, message=error_msg)
#----------------------
# ENDPOINT VERIFICATION
#----------------------
if isinstance(error, EndpointVerificationError):
return Result(valid=False, error_type=ResultError.ENDPOINT_VERIFICATION, message="Operator verification failed. Do you want to continue anyway?")
#----------------------
# STATE CONFLICT
#----------------------
if isinstance(error, ProfileStateConflictError):
# function_choice = literally_connect(
# profile_id=profile_id,
# ignore_state_conflict=True
# )
error_msg = "The profile is already enabled. Do you want to force enable it anyway?"
return Result(valid=False, error_type=ResultError.STATE_CONFLICT, message=error_msg)
#----------------------
# SUBSCRIPTION
#----------------------
if isinstance(error, (InvalidSubscriptionError, MissingSubscriptionError)):
error_msg = f"Subscription missing or invalid for profile {profile_id}. Do you want to get a new sub?"
return Result(valid=False, error_type=ResultError.SUBSCRIPTION, message=error_msg)
#----------------------
# COMMAND NOT FOUND
#----------------------
if isinstance(error, CommandNotFoundError):
# add install later!!
name_of_package = str(error.subject)
error_msg = f"You're missing the {name_of_package} package. Please install it."
return Result(valid=False, error_type=ResultError.MISSING_SOFTWARE, message=error_msg)
else:
return Result(valid=False, error_type=ResultError.UNKNOWN, message="An unknown error occured.")
def get_ignored_exceptions(ignore_state_conflict: Optional[bool] = False):
ignored_as_list = []
endpoint_verification_enabled = ConfigurationController.get_endpoint_verification_enabled()
if endpoint_verification_enabled:
ignored_as_list.append(EndpointVerificationError)
if ignore_state_conflict:
ignored_as_list.append(ProfileStateConflictError)
ignored = tuple(ignored_as_list)
return ignored

View file

@ -1,4 +1,5 @@
from core.models.manage.validate_dataclass import ValidatedDataclass from core.models.manage.validate_dataclass import ValidatedDataclass
from core.utils.file_tools import delete_file
from core.Constants import Constants from core.Constants import Constants
from core.models.enum_models import Currency, ProtocolChoice, PaymentType, PaymentCategory from core.models.enum_models import Currency, ProtocolChoice, PaymentType, PaymentCategory
from core.errors.logger import logger from core.errors.logger import logger
@ -129,6 +130,16 @@ class StatePersistenceMixin(ValidatedDataclass):
setattr(self, field.name, value) setattr(self, field.name, value)
def wipe_data_on_disk(self):
logger.info("Starting wipe of pre-existing data")
if not os.path.exists(self._data_file):
logger.info("File does NOT exist already")
return
return delete_file(self._data_file)
def exists_already(self) -> bool: def exists_already(self) -> bool:
return os.path.exists(self._data_file) return os.path.exists(self._data_file)
@ -163,6 +174,16 @@ class TicketPaymentData(StatePersistenceMixin):
self.load() self.load()
def wipe(self):
"""Wipe all data and start over."""
wiped = self.wipe_data_on_disk()
self.currency = None
self.which_key = None
self.how_many_profiles = 6
self.billing_code = None
self.use_random = False
self.paid = False
sefl.billing_pending = True
@singleton @singleton
@ -192,8 +213,7 @@ class PaymentData(StatePersistenceMixin):
logger.info(f"full path of self._data_file {self._data_file}") logger.info(f"full path of self._data_file {self._data_file}")
self.load() self.load()
@singleton @singleton
@dataclass @dataclass
@ -221,4 +241,3 @@ class OverallPaymentCycle():
self.profile_id = None self.profile_id = None
self.got_invoice = None self.got_invoice = None
self.billing_code = None self.billing_code = None

View file

@ -57,7 +57,6 @@ def mark_as_paid() -> Result:
billing_data.billing_pending = False billing_data.billing_pending = False
billing_data.paid = True billing_data.paid = True
billing_data.save() billing_data.save()
else: else:
return not_supported() return not_supported()

View file

@ -13,6 +13,8 @@ from core.services.prepare_tickets.send_blind_commitments import send_blind_comm
from core.services.prepare_tickets.validate_blind_signatures import validate_blind_signatures from core.services.prepare_tickets.validate_blind_signatures import validate_blind_signatures
from core.services.prepare_tickets.save_ALL_blind_sigs import save_ALL_blind_sigs from core.services.prepare_tickets.save_ALL_blind_sigs import save_ALL_blind_sigs
from core.utils.basic_operations.does_file_exist import does_file_exist from core.utils.basic_operations.does_file_exist import does_file_exist
from core.models.ConfigurationController import ConfigurationController
# errors & constants # errors & constants
from core.Constants import Constants from core.Constants import Constants
from core.errors.exceptions import MissingData, CriticalFailure from core.errors.exceptions import MissingData, CriticalFailure
@ -151,13 +153,18 @@ def ticket_prep_orchestrator(
) )
##################################################### #####################################################
# TICKET TRACKER # TICKET TRACKER & APP-WIDE CONFIG
##################################################### #####################################################
if not does_file_exist(Constants.TICKET_TRACKER_PATH): if not does_file_exist(Constants.TICKET_TRACKER_PATH):
setup_tracker = setup_ticket_tracker(how_many_profiles) setup_tracker = setup_ticket_tracker(how_many_profiles)
if not setup_tracker: if not setup_tracker:
return {"valid": False, "message": "failed"} return {"valid": False, "message": "failed"}
# saving on config that it's setup:
config_marked = ConfigurationController.mark_as_paid(True)
if not config_marked:
return {'valid': False, 'message': 'file_system'}
##################################################### #####################################################
# FINAL EVALUATION # FINAL EVALUATION
##################################################### #####################################################

View file

@ -134,8 +134,7 @@ def start_new_payment_cycle(category: PaymentCategory, profile_id: Optional[int]
return Result(valid=True, data=payment_cycle) return Result(valid=True, data=payment_cycle)
if category == PaymentCategory.INDIVIDUAL: if category == PaymentCategory.INDIVIDUAL:
# Let evaluate_repeat_cycle handle the profile_id comparison # evaluate if it's the same user:
# and the specific "same user" vs "different user" Result errors.
return evaluate_repeat_cycle(payment_cycle=payment_cycle, profile_id=profile_id) return evaluate_repeat_cycle(payment_cycle=payment_cycle, profile_id=profile_id)
# 3. Collision Check (The "Prompt" Logic) # 3. Collision Check (The "Prompt" Logic)
@ -572,3 +571,7 @@ def return_pre_existing_payment_data(profile_id: int, subscription: Subscription
logger.error(error_msg) logger.error(error_msg)
return Result(valid=False, message=error_msg, error_type=ResultError.INVALID_INPUT) return Result(valid=False, message=error_msg, error_type=ResultError.INVALID_INPUT)
def evaluate_ticketing():
setup = ConfigurationController.are_tickets_setup()