@@ -508,14 +508,25 @@ def handle_access_policy_from_labels(rule_key, hostname_config_item):
508508 local_state_changed_by_access_policy = True
509509
510510 app_result = None
511+ app_update_failed = False
511512 if effective_app_id :
512513 logging .info (f"Updating Access App { effective_app_id } for { application_domain } ." )
513- app_result = update_cloudflare_access_application (
514- effective_app_id , application_domain , desired_app_name , desired_session_duration ,
515- desired_app_launcher_visible , [application_domain ], cf_access_policies_or_ids ,
516- desired_allowed_idps , desired_auto_redirect , use_reusable
517- )
518- else :
514+ try :
515+ app_result = update_cloudflare_access_application (
516+ effective_app_id , application_domain , desired_app_name , desired_session_duration ,
517+ desired_app_launcher_visible , [application_domain ], cf_access_policies_or_ids ,
518+ desired_allowed_idps , desired_auto_redirect , use_reusable
519+ )
520+ except Exception as update_error :
521+ error_text = str (update_error )
522+ if "access.api.error.unknown_application" in error_text or "404" in error_text :
523+ logging .info (f"Existing Access App { effective_app_id } not found in Cloudflare (404); will recreate for { application_domain } ." )
524+ app_update_failed = True
525+ else :
526+ logging .error (f"Error updating access app during reconciliation: { update_error } " , exc_info = True )
527+ raise
528+
529+ if not effective_app_id or app_update_failed or not app_result :
519530 logging .info (f"Creating new Access App for { application_domain } ." )
520531 app_result = create_cloudflare_access_application (
521532 application_domain , desired_app_name , desired_session_duration ,
0 commit comments