@@ -113,63 +113,94 @@ def check_and_clear_cached_responses(self):
113113 if len (self .cached_responses ) > 100 :
114114 self .cached_responses = dict ()
115115
116+ async def _call_app_method (
117+ self ,
118+ request ,
119+ * args ,
120+ method_name = None ,
121+ return_val_when_local = None ,
122+ return_val_on_error = None ,
123+ ** kwargs ,
124+ ):
125+ """Function to call an app-method given its name."""
126+ if self .is_local :
127+ # Not applicable to local setups
128+ return return_val_when_local
129+
130+ try :
131+ # Attempt to use app's method; log if this couldn't be done
132+ app_method = getattr (request .app if request else self .app , method_name )
133+ return await app_method (* args , ** kwargs )
134+
135+ except Exception as e :
136+ logging .error (f"Misconfigured app: { method_name } () error: { e } " )
137+ return return_val_on_error
138+
116139 async def action_allowed (self , request , action , context = None ):
117140 """Function to check an action is permitted given a request."""
118141 if self .is_local :
119142 # A permission-checker is not implemented for local-use
120143 # and the user is in control of all their data, so allow the action
121144 return True
145+
122146 try :
123147 # Attempt to use app's method to check permission; log if this couldn't be done
124148 return await request .app .permission_checker (request , action , context )
149+
125150 except (TypeError , AttributeError ) as e :
126- logging .error ("Misconfigured app: permission_checker() error: " + str ( e ) )
151+ logging .error (f "Misconfigured app: permission_checker() error: { e } " )
127152 raise web .HTTPInternalServerError ()
128153
129154 async def url_allowed (self , request , url ):
130155 """Function to check a URL is allowed to be submitted."""
131156 if self .is_local :
132157 # A URL-checker is not implemented for local-use so allow the action
133158 return True
159+
134160 try :
135161 # Attempt to use app's method to check URL; log if this couldn't be done
136162 return await request .app .url_checker (request , url )
163+
137164 except (TypeError , AttributeError ) as e :
138- logging .error ("Misconfigured app: url_checker() error: " + str (e ))
139- # SystemError makes more sense but we are listening for ValueErrors
140- raise ValueError ("Apologies, this URL could not be processed at this time, please contact us." )
165+ logging .error (f"Misconfigured app: url_checker() error: { e } " )
166+ raise SystemError ("Apologies, this URL could not be processed at this time, please contact us." )
141167
142168 async def on_report_complete (self , request , report_data ):
143169 """Function to complete any post-complete actions for a report."""
144- if self .is_local :
145- # No post-complete actions needed for local-use
146- return
147- try :
148- # Attempt to use app's on-complete method; log if this couldn't be done
149- return await request .app .on_report_complete (request , report_data )
150- except (TypeError , AttributeError ) as e :
151- logging .error ("Misconfigured app: on_report_complete() error: " + str (e ))
170+ return await self ._call_app_method (
171+ request ,
172+ request ,
173+ report_data ,
174+ method_name = "on_report_complete" ,
175+ )
176+
177+ async def on_report_error (self , request ):
178+ """Function to complete any submission-error actions for a report."""
179+ return await self ._call_app_method (
180+ request ,
181+ method_name = "on_report_submission_error" ,
182+ )
152183
153184 async def get_current_arachne_user (self , request ):
154185 """Function to obtain the current Arachne username and token given a request."""
155- if self .is_local :
156- return None , None
157- try :
158- # Attempt to use app's method to obtain the username & token; log if this couldn't be done
159- return await request .app .get_current_arachne_user (request )
160- except (TypeError , AttributeError ) as e :
161- logging .error ("Misconfigured app: get_current_arachne_user() error: " + str (e ))
162- return None , None
186+ return await self ._call_app_method (
187+ request ,
188+ request ,
189+ method_name = "get_current_arachne_user" ,
190+ return_val_when_local = (None , None ),
191+ return_val_on_error = (None , None ),
192+ )
163193
164194 async def auto_gen_data_is_valid (self , request , request_data ) -> bool :
165195 """Function to confirm data for an automatically-generated report is valid."""
166- if self .is_local :
167- return True
168- try :
169- return await request .app .auto_gen_data_is_valid (request , request_data )
170- except Exception as e :
171- logging .error ("Misconfigured app: auto_gen_data_is_valid() error: " + str (e ))
172- return False
196+ return await self ._call_app_method (
197+ request ,
198+ request ,
199+ request_data ,
200+ method_name = "auto_gen_data_is_valid" ,
201+ return_val_when_local = True ,
202+ return_val_on_error = False ,
203+ )
173204
174205 async def map_all_html (self , url_input , sentence_limit = None ):
175206 a = newspaper .Article (url_input , keep_article_html = True )
0 commit comments