@@ -320,7 +320,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent):
320320
321321# Increment this PATCH version before using `charmcraft publish-lib` or reset
322322# to 0 if you are raising the major API version
323- LIBPATCH = 22
323+ LIBPATCH = 23
324324
325325PYDEPS = ["ops>=2.0.0" ]
326326
@@ -807,6 +807,9 @@ def _fetch_relation_data_without_secrets(
807807 This is used typically when the Provides side wants to read the Requires side's data,
808808 or when the Requires side may want to read its own data.
809809 """
810+ if app not in relation .data or not relation .data [app ]:
811+ return {}
812+
810813 if fields :
811814 return {k : relation .data [app ][k ] for k in fields if k in relation .data [app ]}
812815 else :
@@ -830,6 +833,9 @@ def _fetch_relation_data_with_secrets(
830833 normal_fields = []
831834
832835 if not fields :
836+ if app not in relation .data or not relation .data [app ]:
837+ return {}
838+
833839 all_fields = list (relation .data [app ].keys ())
834840 normal_fields = [field for field in all_fields if not self ._is_secret_field (field )]
835841
@@ -853,8 +859,11 @@ def _fetch_relation_data_with_secrets(
853859
854860 def _update_relation_data_without_secrets (
855861 self , app : Application , relation : Relation , data : Dict [str , str ]
856- ):
862+ ) -> None :
857863 """Updating databag contents when no secrets are involved."""
864+ if app not in relation .data or relation .data [app ] is None :
865+ return
866+
858867 if any (self ._is_secret_field (key ) for key in data .keys ()):
859868 raise SecretsIllegalUpdateError ("Can't update secret {key}." )
860869
@@ -865,8 +874,19 @@ def _delete_relation_data_without_secrets(
865874 self , app : Application , relation : Relation , fields : List [str ]
866875 ) -> None :
867876 """Remove databag fields 'fields' from Relation."""
877+ if app not in relation .data or not relation .data [app ]:
878+ return
879+
868880 for field in fields :
869- relation .data [app ].pop (field )
881+ try :
882+ relation .data [app ].pop (field )
883+ except KeyError :
884+ logger .debug (
885+ "Non-existing field was attempted to be removed from the databag %s, %s" ,
886+ str (relation .id ),
887+ str (field ),
888+ )
889+ pass
870890
871891 # Public interface methods
872892 # Handling Relation Fields seamlessly, regardless if in databag or a Juju Secret
@@ -880,9 +900,6 @@ def get_relation(self, relation_name, relation_id) -> Relation:
880900 "Relation %s %s couldn't be retrieved" , relation_name , relation_id
881901 )
882902
883- if not relation .app :
884- raise DataInterfacesError ("Relation's application missing" )
885-
886903 return relation
887904
888905 def fetch_relation_data (
@@ -1089,7 +1106,10 @@ def _delete_relation_secret(
10891106 # Remove secret from the relation if it's fully gone
10901107 if not new_content :
10911108 field = self ._generate_secret_field_name (group )
1092- relation .data [self .local_app ].pop (field )
1109+ try :
1110+ relation .data [self .local_app ].pop (field )
1111+ except KeyError :
1112+ pass
10931113
10941114 # Return the content that was removed
10951115 return True
0 commit comments