4141from databricks .sql .backend .databricks_client import DatabricksClient
4242from databricks .sql .backend .kernel ._errors import (
4343 _kernel ,
44- kernel_call as _kernel_call ,
4544 reraise_kernel_error as _reraise_kernel_error ,
45+ wrap_kernel_exception as _wrap_kernel_exception ,
4646)
4747from databricks .sql .backend .kernel .auth_bridge import kernel_auth_kwargs
4848from databricks .sql .backend .kernel .result_set import KernelResultSet
@@ -137,7 +137,7 @@ def open_session(
137137 if session_configuration :
138138 session_conf = {k : str (v ) for k , v in session_configuration .items ()}
139139 try :
140- with _kernel_call ( "open_session" ) :
140+ try :
141141 self ._kernel_session = _kernel .Session (
142142 host = self ._server_hostname ,
143143 http_path = self ._http_path ,
@@ -146,6 +146,8 @@ def open_session(
146146 session_conf = session_conf ,
147147 ** self ._auth_kwargs ,
148148 )
149+ except Exception as exc :
150+ raise _wrap_kernel_exception ("open_session" , exc ) from exc
149151 finally :
150152 # Drop the raw access token from the instance once the
151153 # kernel session is constructed (or failed). The kernel
@@ -222,10 +224,12 @@ def execute_command(
222224 "Statement-level query_tags are not yet supported on the kernel backend."
223225 )
224226
225- with _kernel_call ( "execute_command" ) :
227+ try :
226228 stmt = self ._kernel_session .statement ()
229+ except Exception as exc :
230+ raise _wrap_kernel_exception ("execute_command" , exc ) from exc
227231 try :
228- with _kernel_call ( "execute_command" ) :
232+ try :
229233 stmt .set_sql (operation )
230234 if async_op :
231235 async_exec = stmt .submit ()
@@ -235,6 +239,8 @@ def execute_command(
235239 self ._async_handles [command_id .guid ] = async_exec
236240 return None
237241 executed = stmt .execute ()
242+ except Exception as exc :
243+ raise _wrap_kernel_exception ("execute_command" , exc ) from exc
238244 finally :
239245 # ``Statement`` is a lifecycle owner separate from the
240246 # executed handle it produces. Drop it here so the
@@ -253,8 +259,10 @@ def execute_command(
253259 # can itself raise ``KernelError`` (or, in principle, a PyO3
254260 # native exception) — wrap the construction so callers see a
255261 # mapped PEP 249 exception.
256- with _kernel_call ( "execute_command" ) :
262+ try :
257263 return self ._make_result_set (executed , cursor , command_id )
264+ except Exception as exc :
265+ raise _wrap_kernel_exception ("execute_command" , exc ) from exc
258266
259267 def cancel_command (self , command_id : CommandId ) -> None :
260268 with self ._async_handles_lock :
@@ -266,8 +274,10 @@ def cancel_command(self, command_id: CommandId) -> None:
266274 # Match the Thrift backend's tolerant behaviour.
267275 logger .debug ("cancel_command: no in-flight async handle for %s" , command_id )
268276 return
269- with _kernel_call ( "cancel_command" ) :
277+ try :
270278 handle .cancel ()
279+ except Exception as exc :
280+ raise _wrap_kernel_exception ("cancel_command" , exc ) from exc
271281
272282 def close_command (self , command_id : CommandId ) -> None :
273283 with self ._async_handles_lock :
@@ -279,8 +289,10 @@ def close_command(self, command_id: CommandId) -> None:
279289 if handle is None :
280290 logger .debug ("close_command: no tracked handle for %s" , command_id )
281291 return
282- with _kernel_call ( "close_command" ) :
292+ try :
283293 handle .close ()
294+ except Exception as exc :
295+ raise _wrap_kernel_exception ("close_command" , exc ) from exc
284296
285297 def get_query_state (self , command_id : CommandId ) -> CommandState :
286298 with self ._async_handles_lock :
@@ -296,8 +308,10 @@ def get_query_state(self, command_id: CommandId) -> CommandState:
296308 # ran sync and the result was materialised before
297309 # returning. Terminal by construction.
298310 return CommandState .SUCCEEDED
299- with _kernel_call ( "get_query_state" ) :
311+ try :
300312 state , failure = handle .status ()
313+ except Exception as exc :
314+ raise _wrap_kernel_exception ("get_query_state" , exc ) from exc
301315 if state == "Failed" and failure is not None :
302316 # Surface server-reported failure as a database error so
303317 # the cursor's polling loop terminates with the right
@@ -318,8 +332,10 @@ def get_execution_result(
318332 "get_execution_result called for an unknown command_id; "
319333 "the kernel backend only tracks async-submitted statements."
320334 )
321- with _kernel_call ( "get_execution_result" ) :
335+ try :
322336 stream = async_exec .await_result ()
337+ except Exception as exc :
338+ raise _wrap_kernel_exception ("get_execution_result" , exc ) from exc
323339 # The async-exec handle's role ends once it has produced the
324340 # ``ResultStream`` — keeping it around (and tracked in
325341 # ``_async_handles``) would leak the server-side
@@ -338,9 +354,11 @@ def get_execution_result(
338354 exc ,
339355 )
340356 # ``KernelResultSet.__init__`` calls ``arrow_schema()`` which
341- # can raise — keep that in the mapped-exception scope .
342- with _kernel_call ( "get_execution_result" ) :
357+ # can raise — map that to PEP 249 too .
358+ try :
343359 return self ._make_result_set (stream , cursor , command_id )
360+ except Exception as exc :
361+ raise _wrap_kernel_exception ("get_execution_result" , exc ) from exc
344362
345363 # ── Metadata ───────────────────────────────────────────────────
346364
@@ -382,9 +400,11 @@ def get_catalogs(
382400 ) -> "ResultSet" :
383401 if self ._kernel_session is None :
384402 raise InterfaceError ("get_catalogs requires an open session." )
385- with _kernel_call ( "get_catalogs" ) :
403+ try :
386404 stream = self ._kernel_session .metadata ().list_catalogs ()
387405 return self ._make_result_set (stream , cursor , self ._synthetic_command_id ())
406+ except Exception as exc :
407+ raise _wrap_kernel_exception ("get_catalogs" , exc ) from exc
388408
389409 def get_schemas (
390410 self ,
@@ -397,12 +417,14 @@ def get_schemas(
397417 ) -> "ResultSet" :
398418 if self ._kernel_session is None :
399419 raise InterfaceError ("get_schemas requires an open session." )
400- with _kernel_call ( "get_schemas" ) :
420+ try :
401421 stream = self ._kernel_session .metadata ().list_schemas (
402422 catalog = catalog_name ,
403423 schema_pattern = schema_name ,
404424 )
405425 return self ._make_result_set (stream , cursor , self ._synthetic_command_id ())
426+ except Exception as exc :
427+ raise _wrap_kernel_exception ("get_schemas" , exc ) from exc
406428
407429 def get_tables (
408430 self ,
@@ -417,7 +439,7 @@ def get_tables(
417439 ) -> "ResultSet" :
418440 if self ._kernel_session is None :
419441 raise InterfaceError ("get_tables requires an open session." )
420- with _kernel_call ( "get_tables" ) :
442+ try :
421443 stream = self ._kernel_session .metadata ().list_tables (
422444 catalog = catalog_name ,
423445 schema_pattern = schema_name ,
@@ -448,6 +470,8 @@ def get_tables(
448470 cursor ,
449471 self ._synthetic_command_id (),
450472 )
473+ except Exception as exc :
474+ raise _wrap_kernel_exception ("get_tables" , exc ) from exc
451475
452476 def get_columns (
453477 self ,
@@ -469,14 +493,16 @@ def get_columns(
469493 raise ProgrammingError (
470494 "get_columns requires catalog_name on the kernel backend."
471495 )
472- with _kernel_call ( "get_columns" ) :
496+ try :
473497 stream = self ._kernel_session .metadata ().list_columns (
474498 catalog = catalog_name ,
475499 schema_pattern = schema_name ,
476500 table_pattern = table_name ,
477501 column_pattern = column_name ,
478502 )
479503 return self ._make_result_set (stream , cursor , self ._synthetic_command_id ())
504+ except Exception as exc :
505+ raise _wrap_kernel_exception ("get_columns" , exc ) from exc
480506
481507 # ── Misc ───────────────────────────────────────────────────────
482508
0 commit comments