Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error forwarding from to yp-client from yproxy #75

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading