From a166f6907899243d42c9ead09a2ffe62be64e0c8 Mon Sep 17 00:00:00 2001 From: Christoph Pirkl Date: Wed, 26 Jun 2024 16:27:38 +0200 Subject: [PATCH] Refactor websocket --- pkg/connection/websocket.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pkg/connection/websocket.go b/pkg/connection/websocket.go index 0b10857..3b19be3 100644 --- a/pkg/connection/websocket.go +++ b/pkg/connection/websocket.go @@ -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) @@ -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)) @@ -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))