Skip to content

Commit 963d5b0

Browse files
NiallEgansusodapop
authored andcommitted
Use simple error messages if available + better error message API
This PR has two main aims: 1. Use the `displayMessage` if it has been set on the Thrift response 2. Modify error messages so they have a user-friendly error message and full-context part, giving application builders more flexibility in how they construct any end-messages to the user * New unit tests - Did you add usage logs or metrics? Please mention them here. - Create dashboards or monitoring notebooks? Please link them here. - See http://go/obs/user for docs on our observability tools.
1 parent bcc6754 commit 963d5b0

File tree

5 files changed

+3128
-122
lines changed

5 files changed

+3128
-122
lines changed

cmdexec/clients/python/src/databricks/sql/exc.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1+
import json
2+
import logging
3+
4+
logger = logging.getLogger(__name__)
5+
6+
17
### PEP-249 Mandated ###
28
class Error(Exception):
3-
pass
9+
"""Base class for DB-API2.0 exceptions.
10+
`message`: An optional user-friendly error message. It should be short, actionable and stable
11+
`context`: Optional extra context about the error. MUST be JSON serializable
12+
"""
13+
14+
def __init__(self, message=None, context=None, *args, **kwargs):
15+
super().__init__(message, *args, **kwargs)
16+
self.message = message
17+
self.context = context or {}
18+
19+
def __str__(self):
20+
return self.message
21+
22+
def message_with_context(self):
23+
return self.message + ": " + json.dumps(self.context, default=str)
424

525

626
class Warning(Exception):
@@ -41,5 +61,32 @@ class NotSupportedError(DatabaseError):
4161

4262
### Custom error classes ###
4363
class InvalidServerResponseError(OperationalError):
44-
""" Thrown if the server does not set the initial namespace correctly"""
64+
"""Thrown if the server does not set the initial namespace correctly"""
65+
pass
66+
67+
68+
class ServerOperationError(DatabaseError):
69+
"""Thrown if the operation moved to an error state, if for example there was a syntax
70+
error.
71+
Its context will have the following keys:
72+
"diagnostic-info": The full Spark stack trace (if available)
73+
"operation-id": The Thrift ID of the operation
74+
"""
75+
pass
76+
77+
78+
class RequestError(OperationalError):
79+
"""Thrown if there was a error during request to the server.
80+
Its context will have the following keys:
81+
"method": The RPC method name that failed
82+
"session-id": The Thrift session guid
83+
"query-id": The Thrift query guid (if available)
84+
"http-code": HTTP response code to RPC request (if available)
85+
"error-message": Error message from the HTTP headers (if available)
86+
"original-exception": The Python level original exception
87+
"no-retry-reason": Why the request wasn't retried (if available)
88+
"bounded-retry-delay": The maximum amount of time an error will be retried before giving up
89+
"attempt": current retry number / maximum number of retries
90+
"elapsed-seconds": time that has elapsed since first attempting the RPC request
91+
"""
4592
pass

0 commit comments

Comments
 (0)