Skip to content

Commit 008e852

Browse files
Improve error information when query terminates in unexpected state. (#143)
When a query terminated in either an unknown or an unexpected operation state the connection was creating a driver error with a static message. Updated to include the operation state in the error message for an unknown operation state situation. Updated the case of an unexpected operation state to include the state and the operation status display message. Updated unit tests based on the new error message. Signed-off-by: Raymond Cypher <[email protected]>
2 parents 44d147c + c6902d3 commit 008e852

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

connection.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (c *conn) runQuery(ctx context.Context, query string, args []driver.NamedVa
200200
cli_service.TOperationState_ERROR_STATE,
201201
cli_service.TOperationState_TIMEDOUT_STATE:
202202
logBadQueryState(log, opStatus)
203-
return exStmtResp, opStatus, errors.New(opStatus.GetDisplayMessage())
203+
return exStmtResp, opStatus, unexpectedOperationState(opStatus)
204204
// live states
205205
case cli_service.TOperationState_INITIALIZED_STATE,
206206
cli_service.TOperationState_PENDING_STATE,
@@ -220,16 +220,16 @@ func (c *conn) runQuery(ctx context.Context, query string, args []driver.NamedVa
220220
cli_service.TOperationState_ERROR_STATE,
221221
cli_service.TOperationState_TIMEDOUT_STATE:
222222
logBadQueryState(log, statusResp)
223-
return exStmtResp, statusResp, dbsqlerrint.NewRequestError(ctx, dbsqlerr.ErrInvalidOperationState, nil)
223+
return exStmtResp, statusResp, unexpectedOperationState(statusResp)
224224
// live states
225225
default:
226226
logBadQueryState(log, statusResp)
227-
return exStmtResp, statusResp, dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrInvalidOperationState, nil)
227+
return exStmtResp, statusResp, invalidOperationState(ctx, statusResp)
228228
}
229229
// weird states
230230
default:
231231
logBadQueryState(log, opStatus)
232-
return exStmtResp, opStatus, dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrInvalidOperationState, nil)
232+
return exStmtResp, opStatus, invalidOperationState(ctx, opStatus)
233233
}
234234

235235
} else {
@@ -248,18 +248,27 @@ func (c *conn) runQuery(ctx context.Context, query string, args []driver.NamedVa
248248
cli_service.TOperationState_ERROR_STATE,
249249
cli_service.TOperationState_TIMEDOUT_STATE:
250250
logBadQueryState(log, statusResp)
251-
return exStmtResp, statusResp, dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrInvalidOperationState, nil)
251+
return exStmtResp, statusResp, unexpectedOperationState(statusResp)
252252
// live states
253253
default:
254254
logBadQueryState(log, statusResp)
255-
return exStmtResp, statusResp, dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrInvalidOperationState, nil)
255+
return exStmtResp, statusResp, invalidOperationState(ctx, statusResp)
256256
}
257257
}
258258
}
259259

260260
func logBadQueryState(log *logger.DBSQLLogger, opStatus *cli_service.TGetOperationStatusResp) {
261261
log.Error().Msgf("databricks: query state: %s", opStatus.GetOperationState())
262-
log.Error().Msg(opStatus.GetErrorMessage())
262+
log.Error().Msg(opStatus.GetDisplayMessage())
263+
log.Debug().Msg(opStatus.GetDiagnosticInfo())
264+
}
265+
266+
func unexpectedOperationState(opStatus *cli_service.TGetOperationStatusResp) error {
267+
return errors.WithMessage(errors.New(opStatus.GetDisplayMessage()), dbsqlerr.ErrUnexpectedOperationState(opStatus.GetOperationState().String()))
268+
}
269+
270+
func invalidOperationState(ctx context.Context, opStatus *cli_service.TGetOperationStatusResp) error {
271+
return dbsqlerrint.NewDriverError(ctx, dbsqlerr.ErrInvalidOperationState(opStatus.GetOperationState().String()), nil)
263272
}
264273

265274
func (c *conn) executeStatement(ctx context.Context, query string, args []driver.NamedValue) (*cli_service.TExecuteStatementResp, error) {

connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestConn_executeStatement(t *testing.T) {
161161
if opTest.err == "" {
162162
assert.NoError(t, err)
163163
} else {
164-
assert.EqualError(t, err, "databricks: execution error: failed to execute query: "+opTest.err)
164+
assert.EqualError(t, err, "databricks: execution error: failed to execute query: unexpected operation state "+opTest.state.String()+": "+opTest.err)
165165
}
166166
assert.Equal(t, 1, executeStatementCount)
167167
assert.Equal(t, opTest.closeOperationCount, closeOperationCount)

errors/errors.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package errors
22

33
import (
4+
"fmt"
45
"time"
56

67
"github.com/pkg/errors"
@@ -12,7 +13,6 @@ const (
1213
ErrNotImplemented = "not implemented"
1314
ErrTransactionsNotSupported = "transactions are not supported"
1415
ErrParametersNotSupported = "query parameters are not supported"
15-
ErrInvalidOperationState = "invalid operation state. This should not have happened"
1616
ErrReadQueryStatus = "could not read query status"
1717
ErrSentinelTimeout = "sentinel timed out waiting for operation to complete"
1818

@@ -33,6 +33,14 @@ const (
3333
ErrQueryExecution = "failed to execute query"
3434
)
3535

36+
func ErrInvalidOperationState(state string) string {
37+
return fmt.Sprintf("invalid operation state %s. This should not have happened", state)
38+
}
39+
40+
func ErrUnexpectedOperationState(state string) string {
41+
return fmt.Sprintf("unexpected operation state %s", state)
42+
}
43+
3644
// value to be used with errors.Is() to determine if an error chain contains a request error
3745
var RequestError error = errors.New("Request Error")
3846

0 commit comments

Comments
 (0)