From 6d8b4575933c2390e3a3590581953ae28e27d353 Mon Sep 17 00:00:00 2001 From: SimplifiedPrivacy Date: Thu, 20 Aug 2026 18:00:58 -0400 Subject: [PATCH] Improved error handling and consolidation on ticket purchase flow --- .../tickets/TicketPayController.py | 32 +++---------------- .../services/helpers/get_which_billing_key.py | 20 +++++++----- .../payment_phase/ticket_config_tools.py | 26 ++++++++++++--- .../prepare_tickets/send_blind_commitments.py | 5 +-- core/services/using_tickets/send_unblinded.py | 3 +- .../write_or_read_from_json.py | 4 +-- 6 files changed, 43 insertions(+), 47 deletions(-) diff --git a/core/controllers/tickets/TicketPayController.py b/core/controllers/tickets/TicketPayController.py index cd72473..dc4aa1c 100644 --- a/core/controllers/tickets/TicketPayController.py +++ b/core/controllers/tickets/TicketPayController.py @@ -11,18 +11,14 @@ from core.observers.BaseObserver import BaseObserver from core.services.payment_phase.save_and_send_intitial_billing import save_and_send_intitial_billing from core.services.networking.api_requests.ApiResponseModel import ApiResponse, ErrorType from core.models.Result import Result, ResultError -from core.services.networking.api_requests.step5_solve_api_problems import solve_api_problems from core.errors.logger import logger -# from core.errors.exceptions import NetworkingError from core.services.prepare_tickets.ticket_tracker import does_ticket_tracker_exist from core.services.prepare_tickets.setup_ticket_tracker import setup_ticket_tracker -# from core.services.networking.api_requests.step1_get_or_post import send_data_to_server from core.services.networking.httpx import connect from core.services.networking.make_url import make_url -# from core.utils.confirm_its_a_valid_key_choice import confirm_its_a_valid_key_choice from core.services.helpers.valid_profile_quantity import valid_profile_quantity from core.errors.exceptions import * from core.controllers.tickets.TicketSyncController import sync_ticket_prices @@ -55,11 +51,11 @@ def initiate_payment( made_ticket_tracker = setup_ticket_tracker(how_many_profiles=how_many_profiles) logger.debug(f"Ticket Tracker doesn't exist, so we made it: {made_ticket_tracker} for {how_many_profiles} profiles") - billing_id = do_we_have_billing_id() - if billing_id: - error_msg = "Billing code exists already" - logger.error(error_msg) - return Result(valid=False, error_type=ResultError.BILLING_CODE_EXISTS, data=billing_id, message=error_msg) + billing_id = do_we_have_billing_id() + if billing_id: + error_msg = "Billing code exists already" + logger.error(error_msg) + return Result(valid=False, error_type=ResultError.BILLING_CODE_EXISTS, data=billing_id, message=error_msg) rejected_choices = [None, "", False] @@ -122,8 +118,6 @@ def initiate_payment( except ValueError as e: if ticket_observer: ticket_observer.notify("failed_input", subject=str(e)) - # invoice_data_object.add_error_code("invalid_data") - # return invoice_data_object return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=str(e)) except ConnectionError as e: @@ -131,24 +125,8 @@ def initiate_payment( logger.error(error_msg, exc_info=True) if ticket_observer: ticket_observer.notify("connection_error", subject=str(e)) - # invoice_data_object.add_error_code("connection_error") return Result(valid=False, error_type=ResultError.CONNECTION, message=error_msg) - # except ServerSideError as e: - # error_msg = f"ServerSideError: {e}" - # logger.error(error_msg, exc_info=True) - # ticket_observer.notify("failed_output", subject=error_msg) - # invoice_data_object.add_error_code("server_error") - # return invoice_data_object - - # except Exception as e: - # error_msg = f"Error: {e}" - # logger.error(error_msg, exc_info=True) - # ticket_observer.notify("unknown_error", subject=error_msg) - # invoice_data_object.add_error_code("unknown_error") - # return invoice_data_object - ############### - def check_if_paid( temp_billing_code: str, diff --git a/core/services/helpers/get_which_billing_key.py b/core/services/helpers/get_which_billing_key.py index df36167..e9b61d5 100644 --- a/core/services/helpers/get_which_billing_key.py +++ b/core/services/helpers/get_which_billing_key.py @@ -1,11 +1,15 @@ -from core.utils.basic_operations.write_or_read_from_json import get_value_from_json_file -from core.Constants import Constants +# from core.utils.basic_operations.write_or_read_from_json import get_value_from_json_file +# from core.Constants import Constants -def get_which_billing_key() -> str: - billing_folder = Constants.HV_TICKETING_CONFIG_HOME - filepath = f"{billing_folder}/billing_choices.json" +# def get_which_billing_key() -> str: +# billing_folder = Constants.HV_TICKETING_CONFIG_HOME +# filepath = f"{billing_folder}/billing_choices.json" - json_key = "which_key" - which_key = get_value_from_json_file(filepath, json_key) - return which_key +# json_key = "which_key" +# which_key = get_value_from_json_file(filepath, json_key) +# return which_key + + + +# Function moved to 'ticket_config_tools' module. This is commented out until confirmed stable later. \ No newline at end of file diff --git a/core/services/payment_phase/ticket_config_tools.py b/core/services/payment_phase/ticket_config_tools.py index 281e1b1..2fecf39 100644 --- a/core/services/payment_phase/ticket_config_tools.py +++ b/core/services/payment_phase/ticket_config_tools.py @@ -1,16 +1,32 @@ from core.utils.basic_operations.write_or_read_from_json import get_value_from_json_file from core.Constants import Constants +from core.errors.logger import logger + +billing_folder = Constants.HV_TICKETING_CONFIG_HOME +billing_config = f"{billing_folder}/billing_choices.json" def do_we_have_billing_id() -> str | None: try: - billing_folder = Constants.HV_TICKETING_CONFIG_HOME - filepath = f"{billing_folder}/billing_choices.json" - - billing_id = get_value_from_json_file(filepath, "temp_billing_code") + billing_id = get_value_from_json_file(billing_config, "temp_billing_code") if billing_id == "": return None return billing_id - except: + except ValueError as e: + logger.error(f"We can't open the config, because the input values are incorrect type or form. {e}.") + return False + except KeyError as e: + logger.error(f"The requested value was not found. {e}.") + return None + + +def get_which_billing_key() -> str: + try: + return get_value_from_json_file(billing_config, "which_key") + except ValueError as e: + logger.error(f"We can't open the config to get which_key, because the input values are incorrect type or form. {e}.") + return False + except KeyError as e: + logger.error(f"The requested value of 'which_key' was not found. {e}.") return None diff --git a/core/services/prepare_tickets/send_blind_commitments.py b/core/services/prepare_tickets/send_blind_commitments.py index fd3d86f..5884edc 100644 --- a/core/services/prepare_tickets/send_blind_commitments.py +++ b/core/services/prepare_tickets/send_blind_commitments.py @@ -5,18 +5,15 @@ if TYPE_CHECKING: from essentials.observers.ConnectionObserver import ConnectionObserver from core.observers.TicketObserver import TicketObserver # services -# from core.services.networking.api_requests.step1_get_or_post import send_data_to_server from core.services.networking.httpx import connect from core.services.networking.api_requests.ApiResponseModel import ApiResponse, ErrorType -# from core.services.networking.api_requests.step5_solve_api_problems import solve_api_problems from core.services.networking.make_url import make_url -from core.services.helpers.get_which_billing_key import get_which_billing_key +# from core.services.payment_phase.ticket_config_tools import get_which_billing_key # models & their controllers from core.models.session.SessionProfile import SessionProfile from core.models.system.SystemProfile import SystemProfile from core.models.Subscription import Subscription # not sure if needed -# from core.controllers.ProfileController import ProfileController # utils from core.utils.basic_operations.write_or_read_from_json import get_value_from_json_file diff --git a/core/services/using_tickets/send_unblinded.py b/core/services/using_tickets/send_unblinded.py index 7ec0c0e..759cb5b 100644 --- a/core/services/using_tickets/send_unblinded.py +++ b/core/services/using_tickets/send_unblinded.py @@ -11,7 +11,8 @@ from core.services.networking.api_requests.ApiResponseModel import ApiResponse, from core.services.networking.api_requests.step5_solve_api_problems import solve_api_problems from core.services.networking.make_url import make_url -from core.services.helpers.get_which_billing_key import get_which_billing_key +from core.services.payment_phase.ticket_config_tools import get_which_billing_key + # utils from core.utils.get_raw_string import get_raw_string # errors & generic diff --git a/core/utils/basic_operations/write_or_read_from_json.py b/core/utils/basic_operations/write_or_read_from_json.py index 8723cd0..b7b1d39 100644 --- a/core/utils/basic_operations/write_or_read_from_json.py +++ b/core/utils/basic_operations/write_or_read_from_json.py @@ -107,8 +107,8 @@ def get_value_from_json_file(filepath: str, key: str, category: str | None = Non data = read_entire_json(filepath) if category: - if isinstance(category, bool): - error_msg = f"Error in get_value_from_json_file, category should not be a boolean: {category}" + if not isinstance(category, str): + error_msg = f"Error in get_value_from_json_file, category must be a string, not {type(category)}: {category}" logger.error(error_msg) raise ValueError(error_msg)