@@ -191,6 +191,38 @@ func Test_default_macro_errors(t *testing.T) {
191
191
}
192
192
}
193
193
194
+ func Test_query_panic_recovery (t * testing.T ) {
195
+ cfg := `{ "timeout": 0, "retries": 0, "retryOn": [] }`
196
+ opts := test.DriverOpts {}
197
+
198
+ // Create a macro that triggers a panic
199
+ panicMacro := func (query * sqlds.Query , args []string ) (string , error ) {
200
+ panic ("Random panic for testing purposes" )
201
+ }
202
+ macros := sqlds.Macros {
203
+ "panicTest" : panicMacro ,
204
+ }
205
+
206
+ req , _ , ds := queryRequest (t , "panic-test" , opts , cfg , macros )
207
+
208
+ // Set up a query that uses the panic-triggering macro
209
+ req .Queries [0 ].JSON = []byte (`{ "rawSql": "SELECT $__panicTest() FROM test_table;" }` )
210
+
211
+ // Execute the query
212
+ data , err := ds .QueryData (context .Background (), req )
213
+
214
+ // Verify that the panic was caught and converted to an error
215
+ assert .Nil (t , err )
216
+ assert .NotNil (t , data .Responses )
217
+
218
+ res := data .Responses ["foo" ]
219
+ assert .NotNil (t , res .Error )
220
+ assert .Equal (t , backend .ErrorSourcePlugin , res .ErrorSource )
221
+ assert .Contains (t , res .Error .Error (), "SQL datasource query execution panic" )
222
+ assert .Contains (t , res .Error .Error (), "Random panic for testing purposes" )
223
+ assert .Nil (t , res .Frames )
224
+ }
225
+
194
226
func queryRequest (t * testing.T , name string , opts test.DriverOpts , cfg string , marcos sqlds.Macros ) (* backend.QueryDataRequest , * test.SqlHandler , * sqlds.SQLDatasource ) {
195
227
driver , handler := test .NewDriver (name , test.Data {}, nil , opts , marcos )
196
228
ds := sqlds .NewDatasource (driver )
0 commit comments