Skip to content
Permalink
Browse files

[CALCITE-3275] add nil checks to error parsing (Tino Rusch).

This commit introduces a small helper method to get the server address from the
message metadata. Before using this helper the driver would crash the whole
application if the server sends an error message without setting up the metadata
object.
  • Loading branch information...
trusch authored and F21 committed Aug 20, 2019
1 parent 9e0d6f5 commit 7a35be7713b740eb9bd8e18d2996ca2c279c27f3
Showing with 36 additions and 30 deletions.
  1. +1 −6 connection.go
  2. +7 −8 generic/generic.go
  3. +7 −8 hsqldb/hsqldb.go
  4. +14 −0 message/helper.go
  5. +7 −8 phoenix/phoenix.go
@@ -216,19 +216,14 @@ func (c *conn) avaticaErrorToResponseErrorOrError(err error) error {
return c.adapter.ErrorResponseToResponseError(avaticaErr.message)
}

serverAddress := "unknown"
md := avaticaErr.message.GetMetadata()
if md != nil {
serverAddress = md.ServerAddress
}
return errors.ResponseError{
Exceptions: avaticaErr.message.Exceptions,
ErrorMessage: avaticaErr.message.ErrorMessage,
Severity: int8(avaticaErr.message.Severity),
ErrorCode: errors.ErrorCode(avaticaErr.message.ErrorCode),
SqlState: errors.SQLState(avaticaErr.message.SqlState),
Metadata: &errors.RPCMetadata{
ServerAddress: serverAddress,
ServerAddress: message.ServerAddressFromMetadata(avaticaErr.message),
},
}
}
@@ -109,16 +109,15 @@ func (a Adapter) GetColumnTypeDefinition(col *message.ColumnMetaData) *internal.
return column
}

func (a Adapter) ErrorResponseToResponseError(message *message.ErrorResponse) errors.ResponseError {

func (a Adapter) ErrorResponseToResponseError(err *message.ErrorResponse) errors.ResponseError {
return errors.ResponseError{
Exceptions: message.Exceptions,
ErrorMessage: message.ErrorMessage,
Severity: int8(message.Severity),
ErrorCode: errors.ErrorCode(message.ErrorCode),
SqlState: errors.SQLState(message.SqlState),
Exceptions: err.Exceptions,
ErrorMessage: err.ErrorMessage,
Severity: int8(err.Severity),
ErrorCode: errors.ErrorCode(err.ErrorCode),
SqlState: errors.SQLState(err.SqlState),
Metadata: &errors.RPCMetadata{
ServerAddress: message.GetMetadata().ServerAddress,
ServerAddress: message.ServerAddressFromMetadata(err),
},
}
}
@@ -109,16 +109,15 @@ func (a Adapter) GetColumnTypeDefinition(col *message.ColumnMetaData) *internal.
return column
}

func (a Adapter) ErrorResponseToResponseError(message *message.ErrorResponse) errors.ResponseError {

func (a Adapter) ErrorResponseToResponseError(err *message.ErrorResponse) errors.ResponseError {
return errors.ResponseError{
Exceptions: message.Exceptions,
ErrorMessage: message.ErrorMessage,
Severity: int8(message.Severity),
ErrorCode: errors.ErrorCode(message.ErrorCode),
SqlState: errors.SQLState(message.SqlState),
Exceptions: err.Exceptions,
ErrorMessage: err.ErrorMessage,
Severity: int8(err.Severity),
ErrorCode: errors.ErrorCode(err.ErrorCode),
SqlState: errors.SQLState(err.SqlState),
Metadata: &errors.RPCMetadata{
ServerAddress: message.GetMetadata().ServerAddress,
ServerAddress: message.ServerAddressFromMetadata(err),
},
}
}
@@ -0,0 +1,14 @@
package message

type MetadataProvider interface {
GetMetadata() *RpcMetadata
}

func ServerAddressFromMetadata(message MetadataProvider) string {
serverAddress := "unknown"
md := message.GetMetadata()
if md != nil {
serverAddress = md.ServerAddress
}
return serverAddress
}
@@ -115,14 +115,14 @@ func (a Adapter) GetColumnTypeDefinition(col *message.ColumnMetaData) *internal.
return column
}

func (a Adapter) ErrorResponseToResponseError(message *message.ErrorResponse) errors.ResponseError {
func (a Adapter) ErrorResponseToResponseError(err *message.ErrorResponse) errors.ResponseError {
var (
errorCode int
sqlState string
)

re := regexp.MustCompile(`ERROR (\d+) \(([0-9a-zA-Z]+)\)`)
codes := re.FindStringSubmatch(message.ErrorMessage)
codes := re.FindStringSubmatch(err.ErrorMessage)

if len(codes) > 1 {
errorCode, _ = strconv.Atoi(codes[1])
@@ -132,19 +132,18 @@ func (a Adapter) ErrorResponseToResponseError(message *message.ErrorResponse) er
sqlState = codes[2]
}

err := errors.ResponseError{
Exceptions: message.Exceptions,
ErrorMessage: message.ErrorMessage,
Severity: int8(message.Severity),
return errors.ResponseError{
Exceptions: err.Exceptions,
ErrorMessage: err.ErrorMessage,
Severity: int8(err.Severity),
ErrorCode: errors.ErrorCode(errorCode),
SqlState: errors.SQLState(sqlState),
Metadata: &errors.RPCMetadata{
ServerAddress: message.GetMetadata().ServerAddress,
ServerAddress: message.ServerAddressFromMetadata(err),
},
Name: errorCodeNames[uint32(errorCode)],
}

return err
}

var errorCodeNames = map[uint32]string{

0 comments on commit 7a35be7

Please sign in to comment.
You can’t perform that action at this time.