-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error forwarding from to yp-client from yproxy (#75)
* 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
Showing
5 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters