Skip to content

Commit

Permalink
Add error forwarding from to yp-client from yproxy (#75)
Browse files Browse the repository at this point in the history
* Add error forwarding

* Add ErrorMessage type of message

* Add test for ErrorMessage

* Remove error check from ReadPacket()

* Update proto.go

---------

Co-authored-by: reshke <[email protected]>
  • Loading branch information
robozmey and reshke authored Nov 5, 2024
1 parent 1bc4e5d commit 3d1c950
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
6 changes: 2 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package client

import (
"fmt"
"io"
"net"
"reflect"

"github.com/yezzey-gp/yproxy/pkg/message"
"github.com/yezzey-gp/yproxy/pkg/ylogger"
)

Expand Down Expand Up @@ -76,9 +76,7 @@ func NewYClient(c net.Conn) YproxyClient {
func (y *YClient) ReplyError(err error, msg string) error {
ylogger.Zero.Error().Err(err).Msg(msg)

_, _ = y.Conn.Write([]byte(
fmt.Sprintf("%s: %v", msg, err),
))
_, _ = y.Conn.Write(message.NewErrorMessage(err, msg).Encode())
return nil
}

Expand Down
49 changes: 49 additions & 0 deletions pkg/message/error_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package message

import (
"encoding/binary"
)

type ErrorMessage struct {
Error string
Message string
}

var _ ProtoMessage = &ListMessage{}

func NewErrorMessage(err error, msg string) *ErrorMessage {
return &ErrorMessage{
Error: err.Error(),
Message: msg,
}
}

func (c *ErrorMessage) Encode() []byte {
encodedMessage := []byte{
byte(MessageTypeError),
0,
0,
0,
}

byteError := []byte(c.Error)
byteLen := make([]byte, 8)
binary.BigEndian.PutUint64(byteLen, uint64(len(byteError)))
encodedMessage = append(encodedMessage, byteLen...)
encodedMessage = append(encodedMessage, byteError...)

byteMessage := []byte(c.Message)
binary.BigEndian.PutUint64(byteLen, uint64(len(byteMessage)))
encodedMessage = append(encodedMessage, byteLen...)
encodedMessage = append(encodedMessage, byteMessage...)

binary.BigEndian.PutUint64(byteLen, uint64(len(encodedMessage)+8))
return append(byteLen, encodedMessage...)
}

func (c *ErrorMessage) Decode(data []byte) {
errorLen := binary.BigEndian.Uint64(data[4:12])
c.Error = string(data[12 : 12+errorLen])
messageLen := binary.BigEndian.Uint64(data[12+errorLen : 12+errorLen+8])
c.Message = string(data[12+errorLen+8 : 12+errorLen+8+messageLen])
}
3 changes: 3 additions & 0 deletions pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
MessageTypeGool = MessageType(52)
MessageTypePutV2 = MessageType(53)
MessageTypeCatV2 = MessageType(54)
MessageTypeError = MessageType(55)

DecryptMessage = RequestEncryption(1)
NoDecryptMessage = RequestEncryption(0)
Expand Down Expand Up @@ -59,6 +60,8 @@ func (m MessageType) String() string {
return "COPY"
case MessageTypeGool:
return "GOOL"
case MessageTypeError:
return "ERROR"
}
return "UNKNOWN"
}
20 changes: 20 additions & 0 deletions pkg/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package message_test

import (
"encoding/binary"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -303,3 +304,22 @@ func TestDeleteMsg(t *testing.T) {
assert.True(msg2.Confirm)
assert.True(msg2.Garbage)
}

func TestErrorMsg(t *testing.T) {
assert := assert.New(t)

errString := "could not list objects: NoCredentialProviders: no valid providers in chain."
messageString := "Deprecated. For verbose messaging see aws.Config.CredentialsChainVerboseErrors"
err := fmt.Errorf("%s", errString)

msg := message.NewErrorMessage(err, messageString)
body := msg.Encode()

assert.Equal(body[8], byte(message.MessageTypeError))

msg2 := message.ErrorMessage{}
msg2.Decode(body[8:])

assert.Equal(errString, msg2.Error)
assert.Equal(messageString, msg2.Message)
}
3 changes: 1 addition & 2 deletions pkg/proc/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@ func (r *ProtoReader) ReadPacket() (message.MessageType, []byte, error) {
return 0, nil, err
}

msgType := message.MessageType(data[0])
return msgType, data, nil
return message.MessageType(data[0]), data, nil
}

0 comments on commit 3d1c950

Please sign in to comment.