@@ -175,23 +175,50 @@ def test_related_fields_no_n_plus_1(self):
175175 self .assertEqual (csv_data [2 ][1 ], "" )
176176
177177 def test_callable_error_handling (self ):
178- def _broken_callable (user ):
179- raise ValueError ("test error" )
178+ with self .subTest ("Callable that raises non-CommandError" ):
180179
181- config = {
182- "fields" : ["id" , {"name" : "broken" , "callable" : _broken_callable }],
183- "select_related" : [],
184- "prefetch_related" : [],
185- }
186- self ._create_user ()
187- stderr = StringIO ()
188- with (
189- patch .object (app_settings , "EXPORT_USERS_COMMAND_CONFIG" , config ),
190- self .assertRaises (CommandError ) as context ,
191- ):
192- # the command wraps callable errors in CommandError with callable name
193- call_command ("export_users" , filename = self .temp_file .name , stderr = stderr )
194- self .assertIn ("Error calling function '" , str (context .exception ))
180+ def _broken_callable (user ):
181+ raise ValueError ("test error" )
182+
183+ config = {
184+ "fields" : ["id" , {"name" : "broken" , "callable" : _broken_callable }],
185+ "select_related" : [],
186+ "prefetch_related" : [],
187+ }
188+ self ._create_user ()
189+ stderr = StringIO ()
190+ with (
191+ patch .object (app_settings , "EXPORT_USERS_COMMAND_CONFIG" , config ),
192+ self .assertRaises (CommandError ) as context ,
193+ ):
194+ # the command wraps callable errors in CommandError with callable name
195+ call_command (
196+ "export_users" , filename = self .temp_file .name , stderr = stderr
197+ )
198+ self .assertIn ("Error calling function '" , str (context .exception ))
199+
200+ with self .subTest ("Callable raises CommandError" ):
201+
202+ def _broken_command_error_callable (user ):
203+ raise CommandError ("original msg" )
204+
205+ config2 = {
206+ "fields" : [
207+ "id" ,
208+ {"name" : "broken" , "callable" : _broken_command_error_callable },
209+ ],
210+ "select_related" : [],
211+ "prefetch_related" : [],
212+ }
213+ stderr = StringIO ()
214+ with (
215+ patch .object (app_settings , "EXPORT_USERS_COMMAND_CONFIG" , config2 ),
216+ self .assertRaises (CommandError ) as context2 ,
217+ ):
218+ call_command (
219+ "export_users" , filename = self .temp_file .name , stderr = stderr
220+ )
221+ self .assertIn ("original msg" , str (context2 .exception ))
195222
196223 @patch .object (
197224 app_settings ,
0 commit comments