Skip to content

Commit 6212710

Browse files
committed
made errors more specific
Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent cb1d203 commit 6212710

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/databricks/sql/client.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
OperationalError,
1919
SessionAlreadyClosedError,
2020
CursorAlreadyClosedError,
21+
InterfaceError,
22+
NotSupportedError,
23+
ProgrammingError,
2124
)
2225
from databricks.sql.thrift_api.TCLIService import ttypes
2326
from databricks.sql.thrift_backend import ThriftBackend
@@ -421,7 +424,7 @@ def cursor(
421424
Will throw an Error if the connection has been closed.
422425
"""
423426
if not self.open:
424-
raise Error(
427+
raise InterfaceError(
425428
"Cannot create cursor from closed connection",
426429
connection_uuid=self.get_session_id_hex(),
427430
)
@@ -529,7 +532,7 @@ def __iter__(self):
529532
for row in self.active_result_set:
530533
yield row
531534
else:
532-
raise Error(
535+
raise ProgrammingError(
533536
"There is no active result set",
534537
connection_uuid=self.connection.get_session_id_hex(),
535538
)
@@ -669,7 +672,7 @@ def _close_and_clear_active_result_set(self):
669672

670673
def _check_not_closed(self):
671674
if not self.open:
672-
raise Error(
675+
raise InterfaceError(
673676
"Attempting operation on closed cursor",
674677
connection_uuid=self.connection.get_session_id_hex(),
675678
)
@@ -689,7 +692,7 @@ def _handle_staging_operation(
689692
elif isinstance(staging_allowed_local_path, type(list())):
690693
_staging_allowed_local_paths = staging_allowed_local_path
691694
else:
692-
raise Error(
695+
raise ProgrammingError(
693696
"You must provide at least one staging_allowed_local_path when initialising a connection to perform ingestion commands",
694697
connection_uuid=self.connection.get_session_id_hex(),
695698
)
@@ -719,7 +722,7 @@ def _handle_staging_operation(
719722
else:
720723
continue
721724
if not allow_operation:
722-
raise Error(
725+
raise ProgrammingError(
723726
"Local file operations are restricted to paths within the configured staging_allowed_local_path",
724727
connection_uuid=self.connection.get_session_id_hex(),
725728
)
@@ -749,7 +752,7 @@ def _handle_staging_operation(
749752
handler_args.pop("local_file")
750753
return self._handle_staging_remove(**handler_args)
751754
else:
752-
raise Error(
755+
raise ProgrammingError(
753756
f"Operation {row.operation} is not supported. "
754757
+ "Supported operations are GET, PUT, and REMOVE",
755758
connection_uuid=self.connection.get_session_id_hex(),
@@ -764,7 +767,7 @@ def _handle_staging_put(
764767
"""
765768

766769
if local_file is None:
767-
raise Error(
770+
raise ProgrammingError(
768771
"Cannot perform PUT without specifying a local_file",
769772
connection_uuid=self.connection.get_session_id_hex(),
770773
)
@@ -783,7 +786,7 @@ def _handle_staging_put(
783786
# fmt: on
784787

785788
if r.status_code not in [OK, CREATED, NO_CONTENT, ACCEPTED]:
786-
raise Error(
789+
raise OperationalError(
787790
f"Staging operation over HTTP was unsuccessful: {r.status_code}-{r.text}",
788791
connection_uuid=self.connection.get_session_id_hex(),
789792
)
@@ -803,7 +806,7 @@ def _handle_staging_get(
803806
"""
804807

805808
if local_file is None:
806-
raise Error(
809+
raise ProgrammingError(
807810
"Cannot perform GET without specifying a local_file",
808811
connection_uuid=self.connection.get_session_id_hex(),
809812
)
@@ -813,7 +816,7 @@ def _handle_staging_get(
813816
# response.ok verifies the status code is not between 400-600.
814817
# Any 2xx or 3xx will evaluate r.ok == True
815818
if not r.ok:
816-
raise Error(
819+
raise OperationalError(
817820
f"Staging operation over HTTP was unsuccessful: {r.status_code}-{r.text}",
818821
connection_uuid=self.connection.get_session_id_hex(),
819822
)
@@ -829,7 +832,7 @@ def _handle_staging_remove(
829832
r = requests.delete(url=presigned_url, headers=headers)
830833

831834
if not r.ok:
832-
raise Error(
835+
raise OperationalError(
833836
f"Staging operation over HTTP was unsuccessful: {r.status_code}-{r.text}",
834837
connection_uuid=self.connection.get_session_id_hex(),
835838
)
@@ -1029,7 +1032,7 @@ def get_async_execution_result(self):
10291032

10301033
return self
10311034
else:
1032-
raise Error(
1035+
raise OperationalError(
10331036
f"get_execution_result failed with Operation status {operation_state}",
10341037
connection_uuid=self.connection.get_session_id_hex(),
10351038
)
@@ -1181,7 +1184,7 @@ def fetchall(self) -> List[Row]:
11811184
if self.active_result_set:
11821185
return self.active_result_set.fetchall()
11831186
else:
1184-
raise Error(
1187+
raise ProgrammingError(
11851188
"There is no active result set",
11861189
connection_uuid=self.connection.get_session_id_hex(),
11871190
)
@@ -1198,7 +1201,7 @@ def fetchone(self) -> Optional[Row]:
11981201
if self.active_result_set:
11991202
return self.active_result_set.fetchone()
12001203
else:
1201-
raise Error(
1204+
raise ProgrammingError(
12021205
"There is no active result set",
12031206
connection_uuid=self.connection.get_session_id_hex(),
12041207
)
@@ -1223,7 +1226,7 @@ def fetchmany(self, size: int) -> List[Row]:
12231226
if self.active_result_set:
12241227
return self.active_result_set.fetchmany(size)
12251228
else:
1226-
raise Error(
1229+
raise ProgrammingError(
12271230
"There is no active result set",
12281231
connection_uuid=self.connection.get_session_id_hex(),
12291232
)
@@ -1233,7 +1236,7 @@ def fetchall_arrow(self) -> "pyarrow.Table":
12331236
if self.active_result_set:
12341237
return self.active_result_set.fetchall_arrow()
12351238
else:
1236-
raise Error(
1239+
raise ProgrammingError(
12371240
"There is no active result set",
12381241
connection_uuid=self.connection.get_session_id_hex(),
12391242
)
@@ -1243,7 +1246,7 @@ def fetchmany_arrow(self, size) -> "pyarrow.Table":
12431246
if self.active_result_set:
12441247
return self.active_result_set.fetchmany_arrow(size)
12451248
else:
1246-
raise Error(
1249+
raise ProgrammingError(
12471250
"There is no active result set",
12481251
connection_uuid=self.connection.get_session_id_hex(),
12491252
)

0 commit comments

Comments
 (0)