Skip to content

Commit

Permalink
Refactor websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jun 26, 2024
1 parent d1e610f commit a166f69
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/connection/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,11 @@ func (c *Connection) callback() func(response interface{}) error {
return wrappedError
}

result := &types.BaseResponse{}

reader, err := c.createResponseReader(message)
result, err := c.parseResponse(message)
if err != nil {
return err
}

err = json.NewDecoder(reader).Decode(result)
if err != nil {
wrappedError := errors.NewJsonDecodingError(err, message)
logger.ErrorLogger.Print(wrappedError)
return wrappedError
}

if result.Status != "ok" {
if result.Exception != nil {
return errors.NewSqlErr(result.Exception.SQLCode, result.Exception.Text)
Expand All @@ -156,6 +147,7 @@ func (c *Connection) callback() func(response interface{}) error {
}

if response == nil {
// No response expected
return nil
}
logger.TraceLogger.Printf("Received response with status %q with %d bytes data", result.Status, len(result.ResponseData))
Expand All @@ -167,6 +159,23 @@ func (c *Connection) callback() func(response interface{}) error {
}
}

func (c *Connection) parseResponse(message []byte) (*types.BaseResponse, error) {
result := &types.BaseResponse{}

reader, err := c.createResponseReader(message)
if err != nil {
return nil, err
}

err = json.NewDecoder(reader).Decode(result)
if err != nil {
wrappedError := errors.NewJsonDecodingError(err, message)
logger.ErrorLogger.Print(wrappedError)
return nil, wrappedError
}
return result, nil
}

func (c *Connection) createResponseReader(message []byte) (io.Reader, error) {
if c.Config.Compression {
reader, err := zlib.NewReader(bytes.NewReader(message))
Expand Down

0 comments on commit a166f69

Please sign in to comment.