@@ -84,6 +84,164 @@ def test_basic_capture_with_project_api_key(self):
8484 self .assertEqual (msg ["properties" ]["$lib" ], "posthog-python" )
8585 self .assertEqual (msg ["properties" ]["$lib_version" ], VERSION )
8686
87+ def test_basic_capture_exception (self ):
88+
89+ with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
90+ client = self .client
91+ exception = Exception ("test exception" )
92+ client .capture_exception (exception )
93+
94+ self .assertTrue (patch_capture .called )
95+ capture_call = patch_capture .call_args [0 ]
96+ self .assertEqual (capture_call [0 ], "python-exceptions" )
97+ self .assertEqual (capture_call [1 ], "$exception" )
98+ self .assertEqual (
99+ capture_call [2 ],
100+ {
101+ "$exception_type" : "Exception" ,
102+ "$exception_message" : "test exception" ,
103+ "$exception_list" : [
104+ {
105+ "mechanism" : {"type" : "generic" , "handled" : True },
106+ "module" : None ,
107+ "type" : "Exception" ,
108+ "value" : "test exception" ,
109+ }
110+ ],
111+ "$exception_personURL" : "https://us.i.posthog.com/project/random_key/person/python-exceptions" ,
112+ },
113+ )
114+
115+ def test_basic_capture_exception_with_distinct_id (self ):
116+
117+ with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
118+ client = self .client
119+ exception = Exception ("test exception" )
120+ client .capture_exception (exception , "distinct_id" )
121+
122+ self .assertTrue (patch_capture .called )
123+ capture_call = patch_capture .call_args [0 ]
124+ self .assertEqual (capture_call [0 ], "distinct_id" )
125+ self .assertEqual (capture_call [1 ], "$exception" )
126+ self .assertEqual (
127+ capture_call [2 ],
128+ {
129+ "$exception_type" : "Exception" ,
130+ "$exception_message" : "test exception" ,
131+ "$exception_list" : [
132+ {
133+ "mechanism" : {"type" : "generic" , "handled" : True },
134+ "module" : None ,
135+ "type" : "Exception" ,
136+ "value" : "test exception" ,
137+ }
138+ ],
139+ "$exception_personURL" : "https://us.i.posthog.com/project/random_key/person/distinct_id" ,
140+ },
141+ )
142+
143+ def test_basic_capture_exception_with_correct_host_generation (self ):
144+
145+ with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
146+ client = Client (FAKE_TEST_API_KEY , on_error = self .set_fail , host = "https://aloha.com" )
147+ exception = Exception ("test exception" )
148+ client .capture_exception (exception , "distinct_id" )
149+
150+ self .assertTrue (patch_capture .called )
151+ capture_call = patch_capture .call_args [0 ]
152+ self .assertEqual (capture_call [0 ], "distinct_id" )
153+ self .assertEqual (capture_call [1 ], "$exception" )
154+ self .assertEqual (
155+ capture_call [2 ],
156+ {
157+ "$exception_type" : "Exception" ,
158+ "$exception_message" : "test exception" ,
159+ "$exception_list" : [
160+ {
161+ "mechanism" : {"type" : "generic" , "handled" : True },
162+ "module" : None ,
163+ "type" : "Exception" ,
164+ "value" : "test exception" ,
165+ }
166+ ],
167+ "$exception_personURL" : "https://aloha.com/project/random_key/person/distinct_id" ,
168+ },
169+ )
170+
171+ def test_basic_capture_exception_with_correct_host_generation_for_server_hosts (self ):
172+
173+ with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
174+ client = Client (FAKE_TEST_API_KEY , on_error = self .set_fail , host = "https://app.posthog.com" )
175+ exception = Exception ("test exception" )
176+ client .capture_exception (exception , "distinct_id" )
177+
178+ self .assertTrue (patch_capture .called )
179+ capture_call = patch_capture .call_args [0 ]
180+ self .assertEqual (capture_call [0 ], "distinct_id" )
181+ self .assertEqual (capture_call [1 ], "$exception" )
182+ self .assertEqual (
183+ capture_call [2 ],
184+ {
185+ "$exception_type" : "Exception" ,
186+ "$exception_message" : "test exception" ,
187+ "$exception_list" : [
188+ {
189+ "mechanism" : {"type" : "generic" , "handled" : True },
190+ "module" : None ,
191+ "type" : "Exception" ,
192+ "value" : "test exception" ,
193+ }
194+ ],
195+ "$exception_personURL" : "https://app.posthog.com/project/random_key/person/distinct_id" ,
196+ },
197+ )
198+
199+ def test_basic_capture_exception_with_no_exception_given (self ):
200+
201+ with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
202+ client = self .client
203+ try :
204+ raise Exception ("test exception" )
205+ except Exception :
206+ client .capture_exception ()
207+
208+ self .assertTrue (patch_capture .called )
209+ capture_call = patch_capture .call_args [0 ]
210+ self .assertEqual (capture_call [0 ], "python-exceptions" )
211+ self .assertEqual (capture_call [1 ], "$exception" )
212+ self .assertEqual (capture_call [2 ]["$exception_type" ], "Exception" )
213+ self .assertEqual (capture_call [2 ]["$exception_message" ], "test exception" )
214+ self .assertEqual (capture_call [2 ]["$exception_list" ][0 ]["mechanism" ]["type" ], "generic" )
215+ self .assertEqual (capture_call [2 ]["$exception_list" ][0 ]["mechanism" ]["handled" ], True )
216+ self .assertEqual (capture_call [2 ]["$exception_list" ][0 ]["module" ], None )
217+ self .assertEqual (capture_call [2 ]["$exception_list" ][0 ]["type" ], "Exception" )
218+ self .assertEqual (capture_call [2 ]["$exception_list" ][0 ]["value" ], "test exception" )
219+ self .assertEqual (
220+ capture_call [2 ]["$exception_list" ][0 ]["stacktrace" ]["frames" ][0 ]["filename" ],
221+ "posthog/test/test_client.py" ,
222+ )
223+ self .assertEqual (
224+ capture_call [2 ]["$exception_list" ][0 ]["stacktrace" ]["frames" ][0 ]["function" ],
225+ "test_basic_capture_exception_with_no_exception_given" ,
226+ )
227+ self .assertEqual (
228+ capture_call [2 ]["$exception_list" ][0 ]["stacktrace" ]["frames" ][0 ]["module" ], "posthog.test.test_client"
229+ )
230+
231+ def test_basic_capture_exception_with_no_exception_happening (self ):
232+
233+ with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
234+ with self .assertLogs ("posthog" , level = "WARNING" ) as logs :
235+
236+ client = self .client
237+ client .capture_exception ()
238+
239+ self .assertFalse (patch_capture .called )
240+ self .assertEqual (
241+ logs .output [0 ],
242+ "WARNING:posthog:No exception information available" ,
243+ )
244+
87245 @mock .patch ("posthog.client.decide" )
88246 def test_basic_capture_with_feature_flags (self , patch_decide ):
89247 patch_decide .return_value = {"featureFlags" : {"beta-feature" : "random-variant" }}
0 commit comments