Skip to content

Commit

Permalink
chore: returning errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanCapistrano committed Sep 17, 2023
1 parent 2d3058b commit d4607e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 5 additions & 1 deletion messages/getAllMessagesByIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) {

// Formats the message payload into a custom message type.
func formatMessagePayload(message iotago.Message, messageIndex string) (Message, error) {
payloadInString := utils.SerializeMessagePayload(message.Payload, true)
payloadInString, err := utils.SerializeMessagePayload(message.Payload, true)
if err != nil {
return Message{}, err
}

index := ""
content := ""

Expand Down
14 changes: 7 additions & 7 deletions utils/serialize.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package utils

import (
"errors"
"fmt"
"log"

"github.com/iotaledger/hive.go/serializer"
iotago "github.com/iotaledger/iota.go/v2"
)

// Serializes a given message payload using iota.go package. You can turn
// on/off the debug messages.
func SerializeMessagePayload(messagePayload serializer.Serializable, debugMode bool) string {
func SerializeMessagePayload(messagePayload serializer.Serializable, debugMode bool) (string, error) {
messagePayloadSerialized, err := messagePayload.Serialize(serializer.DeSeriModePerformLexicalOrdering)
if err != nil {
log.Fatal("Unable to serialize the given message payload.")
return "", errors.New("unable to serialize the given message payload")
}

messagePayloadInString := string(messagePayloadSerialized)
Expand All @@ -23,15 +23,15 @@ func SerializeMessagePayload(messagePayload serializer.Serializable, debugMode b
fmt.Printf("Message in string: %s\n\n", messagePayloadInString)
}

return messagePayloadInString
return messagePayloadInString, nil
}

// Serializes a given message using iota.go package. You can turn on/off the
// debug messages.
func SerializeMessage(message iotago.Message, debugMode bool) string {
func SerializeMessage(message iotago.Message, debugMode bool) (string, error) {
messageSerialized, err := message.Serialize(serializer.DeSeriModePerformLexicalOrdering)
if err != nil {
log.Fatal("Unable to serialize the given message.")
return "", errors.New("Unable to serialize the given message")
}

messageInString := string(messageSerialized)
Expand All @@ -41,5 +41,5 @@ func SerializeMessage(message iotago.Message, debugMode bool) string {
fmt.Printf("Message in string: %s\n\n", messageInString)
}

return messageInString
return messageInString, nil
}

0 comments on commit d4607e6

Please sign in to comment.